Use annotations in mybatis to implement additions, deletions, and changes

1. Import the jar package of the unit test

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2. The method of operating the database

We know that adding, deleting, and modifying needs to commit the transaction every time, so the transaction is automatically committed by passing in the parameters in the writing tool class.

public class MybatisUtils {
    
    
    private static SqlSessionFactory sqlSessionFactory;
    static{
    
    
        try {
    
    
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    //默认参数不提交事务
    public static SqlSession getSqlSession() {
    
    
        return getSqlSession(false);
    }
    //如果添加这个参数,就会自动提交事务
    public static SqlSession getSqlSession(boolean flag){
    
    
        return sqlSessionFactory.openSession(flag);
    }
}

3. Annotated sql code

Note: The annotated sql code is the same as the xmlsql code. The interface should be bound in the mybatis-config.xml file

     <mappers>
        <package name="com.li.mapper"/>
    </mappers>
public interface UserMapper {
    
    
    @Select("select * from user")
    //查询所有用户
    List<User> selectUser();

    //根据用户id查询
    User selectById(int id);

    @Select("select * from user where name=#{username} and pwd=#{password}")
    //根据用户姓名,密码查询 两个以上的参数要使用注解
    User selectByUsernamePwd(@Param("username") String username,@Param("password") String password);

    @Insert("insert into user (id,name,pwd) values (#{id},#{name},#{pwd})")
    //增加用户信息
    int addUser(User user);

    @Update({
    
    "update user set name=#{name},pwd=#{pwd} where id=#{id}"})
    //修改用户信息
    int updateUser(User user);

    @Delete("delete from user where id=#{id}")
    //根据id删除用户信息
    int deleteUserById(int id);

}

4. Test

public class UserMapperTest {
    
    

    @Test
    public void testSelectUser() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        for (User user : users) {
    
    
            System.out.println(user);
        }

    }

    @Test
    public void testSelectById() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectById(4);
        System.out.println(user);

    }

    @Test
    public void testSelectByUsernamePwd() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectByUsernamePwd("刘德华", "19600808");
        System.out.println(user);
    }

    @Test
    public void testAddUser() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        int user = mapper.addUser(new User(6, "黄晓明", "66666"));
        System.out.println(user);//增加成功打印 1

    }

    @Test
    public void testUpdateUser() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        int user = mapper.updateUser(new User(6, "郑恺", "456333"));
        System.out.println(user);

    }

    @Test
    public void testDeleteUserById() {
    
    
        //获取session 连接
        SqlSession session = MybatisUtils.getSqlSession(true);
        //获得接口对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        int user = mapper.deleteUserById(3);
        System.out.println(user);
    }

}

扩展: XML and annotations can be used at the same time. Annotations correspond to some complex business logic that cannot be completed. It is difficult to find errors when writing SQL statements.

Guess you like

Origin blog.csdn.net/lirui1212/article/details/105850196