Preparation of traditional dao layer
Written before dao layer, first create a new package com.chy.dao, write interfaces StudentDao:
public interface StudentDao { public void insertStudent(Student student); public void updateStudent(Student student, Integer id); public Student selectStudent(Integer id); }
Then write implementation class StudentDaoImpl:
public class StudentDaoImpl implements StudentDao { @Override public void insertStudent(Student student) { } @Override public void updateStudent(Student student, Integer id) { } @Override public Student selectStudent(Integer id) { return null; } }
Dao layer of the written MyBatis
MyBatis not write this dao. The dao MyBatis consists of two parts: mapping files, map files corresponding interface.
Create a new package com.chy.mapper, New Interface StudentMapper the package:
public interface StudentMapper { public void insertStudent(Student student); public Student selectStudent(Integer id); }
This interface corresponding to the package under the new mapping file StudentMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.chy.mapper.StudentMapper"> <insert id="insertStudent" parameterType="com.chy.pojo.Student"> INSERT INTO student_tb(name,age,score)VALUES (#{name},#{age},#{score}) </insert> <select id="queryById" parameterType="Integer" resultType="Student"> SELECT * FROM student_tb WHERE id=#{id} </select> </mapper>
- Filename mapping file, namespace to interface with the same name, interface-oriented programming.
- id to be the same as the name of the method interface
- Parameter types, the return type for a consistent interface
- Interface methods can only have a maximum of parameters, because the parameter type mapping file can only have a
packet mapper equivalent conventional manner dao package mapping file corresponding to the interface implementation class.
Use MyBatis dao layer
The introduction of mapping file in mybatis global configuration file.
InputStream = Resources.getResourceAsStream the InputStream ( "MyBatis-the config.xml" ); a SqlSessionFactory SqlSessionFactory = new new the SqlSessionFactoryBuilder () Build (inputStream);. The SqlSession SQLSESSION = sqlSessionFactory.openSession (); // call to the interface method by the mapper, the operation database // parameter class object mapper interface classes StudentMapper = sqlSession.getMapper mapper (StudentMapper.class); // insert Student student1 = new new Student (); student1.setName ( "CHy" ); mapper.insertStudent (student1); // query Student student2 = mapper.queryById (1); System.out.println(student2); sqlSession.commit(); sqlSession.close();
Incoming package type parameter
Sometimes we need to pass multiple parameters, such as
- Query age> 20, score> 90 students need to pass two parameters of type Integer
- Query a book in sales this week, need to pass a pojo category: Book, also need to pass a parameter representation within this week.
- To query a user purchased a motor vehicle order information, need to pass two pojo categories: User, Car
This situation is more common multiple parameters passed in complex conditional queries, especially multi-table queries.
mybatis can pass up to one argument, how do?
Can be put to the parameters passed packaged, placed in a packaging, the packaging can be passed .
New Package com.chy.vo, under the new package wrapper class UserQueryVO, write parameters to be passed in as a member variable, and provides getter, setter methods:
public class UserQueryVO { private User user; private Car car; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } }
vo: value object value object
po: persist object persistent object, corresponding to the data table.
pojo: plain ordinary java object simple Java objects
New interfaces at com.chy.mapper UserMapper packet, write the corresponding mapping file implement database operations.
public interface UserMapper { public Order queryOrder(UserQueryVO vo); }
<mapper namespace="com.chy.mapper.UserMapper"> <select id="queryOrder" parameterType="com.chy.vo.UserQueryVO" resultType="com.chy.pojo.Order"> SELECT * FROM order_tb WHERE order_tb.user_id=#{user.id} AND order_tb.car_name=#{car.name} </select> </mapper>
# {} In the user, CAR is vo property, the two properties are themselves objects, can be accessed by user., CAR properties.
use:
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession=sqlSessionFactory.openSession(); Car car = new Car(); car.setName("宝马X6"); User user = new User(); user.setId(1); //要传入的包装类 UserQueryVO vo = new UserQueryVO(); vo.setCar(car); vo.setUser(user); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //传入包装类进行查询 Order order = mapper.queryOrder(vo); System.out.println(order);
Using a number of parameters passed in Map
In addition to using a wrapper class pass multiple parameters, you can use Map pass multiple parameters.
Mapper interfaces:
public interface UserMapper { public Order queryOrder(Map<String,Object> map); }
This is the Mapper interface, the interface parameters usually write, do not write a specific category, so that coupling is the interface level.
Different data types to be passed parameters, written in Object.
Mapping file:
<mapper namespace="com.chy.mapper.UserMapper"> <select id="queryOrder" parameterType="HashMap" resultType="com.chy.pojo.Order"> SELECT * FROM order_tb WHERE order_tb.user_id=#{user.id} AND order_tb.car_name=#{car.name} </select> </mapper>
# {} In the user, car map is the key, obtain a corresponding value (the object), and then through. Getting a property value.
use:
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession=sqlSessionFactory.openSession(); Car car = new Car(); car.setName("宝马X6"); User user = new User(); user.setId(1); //要传入的包装类 HashMap<String,Object> map = new HashMap<>(); map.put("car", car); map.put("user", user); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //传入map进行查询 Order order = mapper.queryOrder(map); System.out.println(order);
Compared with the use of packaging a number of parameters passed in class, using the map need to create vo classes easier.