The statement

This blog post is still based on notes from the dark Horse Programmer’s course.

1. Implement CRUD operations

1.1 Steps for setting up Mybatis environment

  • Step 1: Create a Maven project
  • Step 2: Import coordinates
  • Step 3: Write the necessary code (entity classes and persistence layer interfaces)
  • Step 4: Write the SQLmapconfig.xml global configuration file
  • Step 5: Write the mapping configuration file
  • Step 6: Write the test class

Roughly as shown in the figure:

These sections are more easily covered in the introductory case in the previous blog, so check them out if you need to.

There is a requirement that the attribute names in the entity class be exactly the same as the column names in the database, namely:

Some people may ask: this is not reasonable, why must be the same, in real development, these work may be separate, will encounter different situations. How to solve the difference? The solution to this problem will be solved later.

After setting up the environment, the general operations to be done are only written in the last file in the figure.

1.2 to add

1. Add a method to the interface file:

/** * saveUser * @param user */ void saveUser(user user);Copy the code

2. Write SQL statements in the mapping configuration file

3. Write code in a test class

/** * Test public void testSave() throws IOException {User User = new User(); user.setUsername("zhouysan"); user.setAddress("guigang"); user.setSex("m"); user.setBirthday(new Date()); System.out.println(user); Userdao.saveuser (user); //5. System.out.println(user); }Copy the code

Attention!!

Why not read the global configuration file, create the SessionFactory, create the DAO proxy object, and free the resources?

Because this part of the code is used in every method, it is presented for development efficiency.

1.3 delete and change

Because delete and change is relatively simple, need to pay attention to the point and the above increase is the same, so put together directly paste code.

1. Add a method to the interface file:

@param userId */ void deleteUser(Integer userId); /** * updateUser * @param user */ void updateUser(user user);Copy the code

2. Write SQL statements in the mapping configuration file

<! ParameterType ="Integer"> Delete from user where id=#{id} </delete> <! ParameterType ="com.zhouman.domain.User"> updateUser set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id} </update>Copy the code

3. Write code in a test class

*/ @test public void deleteUser() throws IOException {//5. Execute method userdao.deleteUser (48); } /** * Test public void updateUser() throws IOException {User User = new User(); user.setId(54); user.setUsername("zhouyMO"); user.setAddress("guigang"); user.setSex("m"); user.setBirthday(new Date()); //5. Run userdao.updateUser (user); }Copy the code

1.4 the query

1. Add a method to the interface file:

/** * Query a User by id * @param uerId * @return */ User findById(Integer uerId); @param username * @return */ List<User> findByName(String username); @return */ int findTotal();Copy the code

2. Write SQL statements in the mapping configuration file

<! <select ID ="findById" parameterType="INT" resultType="com.zhouman.domain.User"> select * from User where id=#{id} </select> <! <select ID ="findByName" parameterType="String" resultType="com.zhouman.domain.User"> select * from user where username like #{username} </select> <! ResultType ="int"> select count(id) from user </select>Copy the code

3. There is one detail in the fuzzy query method that is coded in the test class, that is to add %, or otherwise to concatenate % in the XML file (which I find cumbersome).

/** * Tests query operations based on id */ @test public void testFindOne() throws IOException {//5. User User = userdao.findByID (54); System.out.println(user); */ @test public void testfindByName() throws IOException {//5. List<User> users = userdao.findByName ("% king %"); for (User user: users) { System.out.println(user); }} /** * Queries the total number of records */ @test public void testfindTotal() throws IOException {//5. Int total = userdao.findTotal (); System.out.println(total); }Copy the code

2. Package the output results of Mybatis

Answer the previous blog question: What happens when the attribute name of the entity class is different from the table column name of the database?

2.1 Method 1: Alias the class when writing SQL statements in XML.

<! <select id="findAll" resultType="com.itheima.domain.User"> select ID as userId,username as userName,birthday as userBirthday,sex as userSex,address as userAddress from user </select>Copy the code

I’m going to draw that, so that it fits.But this method is high execution efficiency, but the development efficiency is not high, the need to alias SQL statements have to write, that is quite troublesome. So there has to be another way

2.2 Method 2:

1. Define resultMap Add the following labels to

<resultMap type="com.itheima.domain.User" id="userMap"> 
    <id column="id" property="userId"/>
    <result column="username" property="userName"/>
    <result column="sex" property="userSex"/>
    <result column="address" property="userAddress"/>
    <result column="birthday" property="userBirthday"/>
</resultMap>
Copy the code

2. Invoke the resultMap content

Take one of these methods for example