Write before:
Continue to record your MyBatis learning journey. If you don't understand it, it is recommended to read it first Previous blogs , detailed codes can be found in My Gitee warehouse SSM learning Clone download learn to use!
3.11 annotation development
3.11.1 notes
MyBatis can also be developed with annotations to reduce the writing of Mapper mapping files. Common annotations are as follows:
annotation | explain |
---|---|
@Insert | Realize new |
@Update | Implementation update |
@Delete | Implementation deletion |
@Select | Implement query |
@Result | Implement result set encapsulation |
@Results | Use with @ Result to realize multiple Result sets |
@One | Realize one-to-one result encapsulation |
@Many | Implement one to many result encapsulation |
3.11.2 project preparation
and ####3.10.1.2 project preparation The same, except there is no need to configure UserMapper and ordermapper XML file!
3.11.3 code implementation basic CURD
3.11.3.1 create UserMapper interface
Create a UserMapper interface class, write the CURD function and add comments, as follows:
@Select("select * from user") List<User> findAll(); @Select("select * from user where id = #{id}") User findById(int id); @Insert("insert into user (id,name,password) values(#{id},#{name},#{password})") int insert(User user); @Update("update user set name = #{name},password = #{password} where id = #{id}") int update(User user); @Delete("delete from user where id = #{id}") int delete(int id);
3.11.3.2 add mapping relationship
In sqlmapperconfig Add the mapping relationship of interface annotation in XML, as follows:
<!-- Load mapping relationship--> <mappers> <!-- Specify the package where the interface is located--> <package name="com.demo.mapper"/> </mappers>
3.11.3.3 writing test classes
Write test code in the test class, including simple addition, deletion, modification and query operations, as follows:
private UserMapper userMapper; @Before public void before() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sessionFactory.openSession(true); userMapper = sqlSession.getMapper(UserMapper.class); } @Test public void testInsert() { User user = new User(); user.setId(7); user.setName("Wang San"); user.setPassword("adada"); userMapper.insert(user); } @Test public void testDelete() { userMapper.delete(6); } @Test public void testUpdate() { User user = new User(); user.setId(7); user.setName("Wang Liu"); user.setPassword("qwerqr"); userMapper.update(user); } @Test public void testSelectAll() { List<User> userMapperAll = userMapper.findAll(); for (User user:userMapperAll) { System.out.println(user); } } @Test public void testSelect() { User user = userMapper.findById(1); System.out.println(user); }
3.11.1.3.4 testing
When you run the test function, you can see that the addition is successful in the database, as shown in the figure
When the delete function is run, the result is as shown in the figure
When the modified function is run, the result is as shown in the figure
When a single function is queried, the results are shown in the figure below
When all functions are queried, the results are shown in the figure below
3.11.4 complex mapping development
3.11.4.1 realize the development of complex mapping relationship
Without annotation, it is generally implemented by configuring < resultmap > in the mapping file. After annotation, the following annotations can be used to realize the configuration of complex relationships.
annotation | explain |
---|---|
@Results | Instead of the tag < resultmap >, a single @ Result or @ Result set can be used in this annotation. Use format: @ Result({@Result(),@Result()}) or |
Result | Replaced the < ID > tag and < Result > tag@ Description of attributes in Result: column: database column name; Property: the property name in the entity class; One: the @ one annotation to be used (@ Result (one = @ one())); Many: required @ many annotation (@ Result (many = @Many) ()) |
3.11.4.2 one to one code operation
3.11.4.2.1 interface operation
Write function functions and annotations in the OrderMapper interface, as follows:
// Method 1 // @Select("select *,o.id oid from orders o,user u where o.uid = u.id") // @Results( // { @Result(column = "oid",property = "id"), // @Result(column = "ordertime",property = "orderTime"), // @Result(column = "total",property = "total"), // @Result(column = "uid",property = "user.id"), // @Result(column = "name",property = "user.name"), // @Result(column = "password",property = "user.password"), // @Result(column = "birthday",property = "user.birthday")} // ) // Method 2 @Select("select * from orders") @Results( { @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "orderTime"), @Result(column = "total",property = "total"), @Result( // Entity type to encapsulate javaType = User.class, // Encapsulate attribute name property = "user", // Query the data of user table according to this field column = "uid", // The Select attribute represents the method of querying that interface to obtain data one = @One(select="com.demo.mapper.UserMapper.findById") ) } ) List<Order> findAll();
3.11.4.2.2 test
The test is similar to the basic operation of CURD, and the results are shown in the figure
3.11.4.3 code implementation: one to many
3.11.4.3.1 interface operation
Write a search function based on Id in the OrderMapper interface, as follows:
@Select("select * from orders where uid = #{uid}") Order findByUid(int id);
Write a one to many query function in the UserMapper interface, as follows:
@Select("select * from user") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "password",property = "password"), @Result(column = "birthday",property = "birthday"), @Result( property = "userOrders", column = "id", javaType = List.class, many = @Many(select = "com.demo.mapper.OrderMapper.findByUid") ) }) List<User> findAllUserOrders();
3.11.4.3.2 testing
The test is similar to that before, and the results are shown in the figure
3.11.4.4 code implementation many to many
3.11.4.4.1 interface operation
Create the RoleMapper interface, where the lookup function based on Id is written, as follows:
@Select("select * from user_role ur,role r where ur.role_id=r.id and ur.user_id = #{id}") List<Role> findByUid(int id);
Add the many to many query function in the UserMapper interface, as follows:
@Select("select * from user") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "password",property = "password"), @Result( property = "roleList", javaType = List.class, column = "id", many = @Many(select = "com.demo.mapper.RoleMapper.findByUid") ) }) List<User> findAllUserAndRole();
3.11.4.4.2 testing
The test is similar to that before, and the results are shown in the figure