• The last article completed the construction of the MyBatis environment and the first MyBatis program preparation of the first MyBatis program, now on this basis, to achieve the increase, deletion, change and check database
  • For the following operations, you only need to modify interfacesUserMapperAnd XML configuration filesUserMapper.xmlCan be
  • It is worth mentioning that if IDEA is connected to the database, the following mapper file has code prompts when writing SQL statements

SELECT operation

  • Add a query method to the interface, which is queried by User’s ID
User getUserById(Integer id);
Copy the code
  • Usermapper. XML in the namespace isUserMapperthemapperNext, add oneselectThe module
<select id="getUserById" resultType="com.du.mybatis.pojo.User" parameterType="int">
    select id, `name`, address
    from user
    where id = #{id};
</select>
Copy the code
  • idIs the corresponding method name,resultTypeIs the returned parameter type,parameterTypeEnter the parameter type for the method
  • Input parameters pass#{id}To obtain
  • The test method
  @Test
  public void getUserByIdTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      User userById = mapper.getUserById(2); System.out.println(userById); }}Copy the code

INSERT, INSERT

  • Insert method
int insertUser(User user);
Copy the code
  • joininsertThe module
<insert id="insertUser" parameterType="com.du.mybatis.pojo.User">
    insert into user(id, name, address)
    values (#{id}, #{name}, #{address});
</insert>
Copy the code
  • The test method
  @Test
  public void insertUserTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.insertUser(new User(10."Li bai"."India"));
      if (res > 0)
        System.out.println("Insert successful");
      else
        System.out.println("Insert failed");
      sqlSession.commit(); // Commit transaction, must!}}Copy the code
  • A transaction must be committed if it involves modifying the database!!

UPDATE, modify operation

  • Change the method
int updateUser(User user);
Copy the code
  • joinupdateThe module
<update id="updateUser" parameterType="com.du.mybatis.pojo.User">
    update user
    set name=#{name},
        address = #{address}
    where id = #{id};
</update>
Copy the code
  • The test code
  @Test
  public void updateUserTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.updateUser(new User(3."Du fu"."Tibet"));
      if (res > 0)
        System.out.println("Update successful");
      else
        System.out.println("Update failed"); sqlSession.commit(); }}Copy the code

4. DELETE

  • Delete methods
int deleteUserByID(int id);
Copy the code
  • joindeleteThe module
<delete id="deleteUserByID" parameterType="int">
    delete
    from user
    where id = #{id};
</delete>
Copy the code
  • The test code
  @Test
  public void deleteUserByIdTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.deleteUserByID(2);
      if (res > 0)
        System.out.println("Deleted successfully");
      else
        System.out.println("Delete failed"); sqlSession.commit(); }}Copy the code

Five, multi-parameter input, passMapimplementation

  • Delete method. The input parameter ismap
int deleteUserByNameAndAddress(Map<String, String> map);
Copy the code
  • joindeleteThe module
<delete id="deleteUserByNameAndAddress" parameterType="map">
    delete
    from user
    where name = #{name}
      and address = #{address};
</delete>
Copy the code
  • parameterTypeMap (lowercase, which is an alias)
  • Directly in the# {}Middle finger is good
  • The test code
  @Test
  public void deleteUserByNameAndAddressTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      Map<String, String> map = new HashMap<String, String>();
      map.put("name"."Li bai");
      map.put("address"."India");
      int res = mapper.deleteUserByNameAndAddress(map);
      if (res > 0)
        System.out.println("Deleted successfully");
      else
        System.out.println("Delete failed"); sqlSession.commit(); }}Copy the code

Vi. Complete code

public interface UserMapper {
  /** * query all users **@return* /
  List<User> getUserList(a);

  /** * query User ** by ID@return* /
  User getUserById(Integer id);

  /** * insert a User **@param user
   * @return0 indicates failure; >0 indicates success */
  int insertUser(User user);

  /** * delete User ** by ID@param id
   * @return* /
  int deleteUserByID(int id);

  /** * Update user ** based on ID@param user
   * @return* /
  int updateUser(User user);

  /** * Delete element * based on name and address@param map
   * @return* /
  int deleteUserByNameAndAddress(Map<String, String> map);
}
Copy the code

      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<! -- Change to the corresponding interface -->
<mapper namespace="com.du.mybatis.dao.UserMapper">
    <! -- id = method name in interface -->
    <! ResultType = query bean, remember full path! -->
    <select id="getUserList" resultType="com.du.mybatis.pojo.User">
        select id, `name`, address
        from user;
    </select>
    <select id="getUserById" resultType="com.du.mybatis.pojo.User" parameterType="int">
        select id, `name`, address
        from user
        where id = #{id};
    </select>
    <insert id="insertUser" parameterType="com.du.mybatis.pojo.User">
        insert into user(id, name, address)
        values (#{id}, #{name}, #{address});
    </insert>
    <delete id="deleteUserByID" parameterType="int">
        delete
        from user
        where id = #{id};
    </delete>
    <update id="updateUser" parameterType="com.du.mybatis.pojo.User">
        update user
        set name=#{name},
            address = #{address}
        where id = #{id};
    </update>
    <delete id="deleteUserByNameAndAddress" parameterType="map">
        delete
        from user
        where name = #{name}
          and address = #{address};
    </delete>
</mapper>
Copy the code
public class UserMapperTest {
  @Test
  public void getUserListTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      List<User> userList = mapper.getUserList();
      for(User user : userList) { System.out.println(user); }}}@Test
  public void getUserByIdTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      User userById = mapper.getUserById(2); System.out.println(userById); }}@Test
  public void insertUserTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.insertUser(new User(10."Li bai"."India"));
      if (res > 0)
        System.out.println("Insert successful");
      else
        System.out.println("Insert failed");
      sqlSession.commit(); // Commit transaction, must!}}@Test
  public void deleteUserByIdTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.deleteUserByID(2);
      if (res > 0)
        System.out.println("Deleted successfully");
      else
        System.out.println("Delete failed"); sqlSession.commit(); }}@Test
  public void updateUserTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      int res = mapper.updateUser(new User(3."Du fu"."Tibet"));
      if (res > 0)
        System.out.println("Update successful");
      else
        System.out.println("Update failed"); sqlSession.commit(); }}@Test
  public void deleteUserByNameAndAddressTest(a) {
    try (SqlSession sqlSession = Utils.getSqlSession()) {
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      Map<String, String> map = new HashMap<String, String>();
      map.put("name"."Li bai");
      map.put("address"."India");
      int res = mapper.deleteUserByNameAndAddress(map);
      if (res > 0)
        System.out.println("Deleted successfully");
      else
        System.out.println("Delete failed"); sqlSession.commit(); }}}Copy the code