使用mybatis完成简单的crud操作

目录

1.配置核心配置文件和jdbc.properties

2、 装mybatis工具类

3、创建实体类

4、创建mapper接口

5、编写测试类


下面介绍基于注解的方式实现基本的 CRUD 操作

1.配置核心配置文件和jdbc.properties

记扫描mapper包!!!




    
    

    
    
        
            
            
                
                
                
                
            
        
    

    


2、 装mybatis工具类

如果代码书写正确但数据库没有写入数据,记得检查是否提交了事务(sqlsession.commit)

public class MybatisUtil {
    private static  SqlSessionFactory sqlSessionFactory;
    static {
        String resource ="mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession( );
    }

    public static void closeSqlSession(SqlSession sqlSession){
        sqlSession.commit();
        if (sqlSession != null){
            sqlSession.close();
        }

    }}

3、创建实体类

public class User {   
 Integer id;
    String username;
    String password;
    Integer age;
}
//省略get set方法

4、创建mapper接口

在mapper接口中,使用注解的形式写sql语句。


@Mapper
public interface UserMapper {

    //查询用户
   @Select("select * from user")
    List getAll();

   //通过id查询用户
    @Select("select * from user where id = #{id}")
    User selectUserById(Integer id);

    //通过name查询用户
    @Select("select * from user where username = #{name}")
    List selectUserByName(String name);

    //添加用户
    @Insert("insert into user(username,password) values ( #{username}, #{password} )")
    int insertUser(User user);

    //修改用户
    @Update("update user set password=#{password} where id = #{id}")
    void updateUser(User user);
}

5、编写测试类

public class MyTest {
    @Test
    public  void getAllTest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List all = mapper.getAll();
        MybatisUtil.closeSqlSession(sqlSession);
        for (User list:all){
            System.out.println(list);
        }

        }
    @Test
    public  void getUserByIdTest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserById(4);
        System.out.println(user);
    }
    @Test
    public void getUserByNameTest(){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List admin = (List) mapper.selectUserByName("admin");
        System.out.println(admin);
    }
    @Test
    public void insertUserTest() throws IOException {
        User user = new User();
        user.setUsername("pp");
        user.setPassword("123");
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.insertUser(user);
        sqlSession.commit();

    }
    @Test
    public void updateUserTest(){
        User user = new User();
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        user.setPassword("000");
        user.setId(4);
        mapper.updateUser(user);
        MybatisUtil.closeSqlSession(sqlSession);
    }

}

你可能感兴趣的:(spring,java)