Mybatis单独使用(原始的Dao开发和Mapper代理方法开发)
原始的Dao开发方法
1.UserDao 定义接口
2.UserDaoImpl 构造参数初始化属性SqlSessionFactory,方法的局部变量通过SqlSessionFactory生成SqlSession
3.UserDaoImplTest 创建一个SqlSessionFactory对象,作为参数创建一个UserDaoImpl对象UserDao userDao = new UserDaoImpl(sqlSessionFactory); 然后调用UserDaoImpl的方法User user = userDao.findUserById(1);
/** * UserDao */ public interface UserDao { //根据id查询用户信息 public User findUserById(int id) throws Exception; //根据用户名列查询用户列表 public List<User> findUserByName(String name) throws Exception; //添加用户信息 public void insertUser(User user) throws Exception; //删除用户信息 public void deleteUser(int id) throws Exception; }
/** * * <p> * Title: UserDaoImpl */ /** * 因为sqlSession是线程不安全的,所以最佳应用场合是在方法体内,定义成局部变量使用 * */ public class UserDaoImpl implements UserDao { // 需要向dao实现类中注入SqlSessionFactory // 这里通过构造方法注入 private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public User findUserById(int id) throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("test.findUserById", id); // 释放资源 sqlSession.close(); return user; } @Override public List<User> findUserByName(String name) throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); List<User> list = sqlSession.selectList("test.findUserByName", name); // 释放资源 sqlSession.close(); return list; } @Override public void insertUser(User user) throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //执行插入操作 sqlSession.insert("test.insertUser", user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } @Override public void deleteUser(int id) throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //执行插入操作 sqlSession.delete("test.deleteUser", id); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } }
/** *UserDaoImplTest */ public class UserDaoImplTest { private SqlSessionFactory sqlSessionFactory; // 此方法是在执行testFindUserById之前执行 @Before public void setUp() throws Exception { // 创建sqlSessionFactory // mybatis配置文件 String resource = "SqlMapConfig.xml"; // 得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); // 创建会话工厂,传入mybatis的配置文件信息 sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); } @Test public void testFindUserById() throws Exception { // 创建UserDao的对象 UserDao userDao = new UserDaoImpl(sqlSessionFactory); // 调用UserDao的方法 User user = userDao.findUserById(1); System.out.println(user); } }
Mapper代理的方法
1.XxxMapper.java接口
2.XxxMapper.xml 省略未列出
3.UserMapperTest.java
/** * * Mapper接口(Dao接口) */ public interface UserMapper { //用户信息综合查询 public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception; //用户信息综合查询总数 public int findUserCount(UserQueryVo userQueryVo) throws Exception; //根据id查询用户信息 public User findUserById(int id) throws Exception; //根据id查询用户信息,使用resultMap输出 public User findUserByIdResultMap(int id) throws Exception; //根据用户名列查询用户列表 public List<User> findUserByName(String name)throws Exception; //插入用户 public void insertUser(User user)throws Exception; //删除用户 public void deleteUser(int id)throws Exception; //更新用户 public void updateUser(User user)throws Exception; }
public class UserMapperTest { private SqlSessionFactory sqlSessionFactory; // 此方法是在执行testFindUserById之前执行 @Before public void setUp() throws Exception { // 创建sqlSessionFactory // mybatis配置文件 String resource = "SqlMapConfig.xml"; // 得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); // 创建会话工厂,传入mybatis的配置文件信息 sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); } //用户信息的综合 查询 @Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件 UserQueryVo userQueryVo = new UserQueryVo(); UserCustom userCustom = new UserCustom(); //由于这里使用动态sql,如果不设置某个值,条件不会拼接在sql中 // userCustom.setSex("1"); userCustom.setUsername("小明"); //传入多个id List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(10); ids.add(16); //将ids通过userQueryVo传入statement中 userQueryVo.setIds(ids); userQueryVo.setUserCustom(userCustom); //调用userMapper的方法 List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println(list); } }