多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 注意 这里需要使用之前配置的IUserDao.xml文件. ## 查询所有 新建UserDaoImpl类 ~~~ package com.like.daoimpl; import com.like.dao.IUserDao; import com.like.domain.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class UserDaoImpl implements IUserDao { private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public List<User> findAll() { SqlSession session = sqlSessionFactory.openSession(); List<User> users = session.selectList("com.like.dao.IUserDao.findAll"); //这里写xml 的id session.close(); return users; } @Override public void saveUser(User user) { } @Override public void updateUser(User user) { } @Override public void deletedUser(Integer id) { } @Override public User findById(Integer id) { return null; } @Override public List<User> findByName(String username) { return null; } @Override public int findTotal() { return 0; } } ~~~ ~~~ //读取配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //创建SqlSessionFactory工厂 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); IUserDao userDao = new UserDaoImpl(build); List<User> all = userDao.findAll(); System.out.println(all); ~~~ ## 保存数据 ~~~ public void saveUser(User user) { SqlSession session = sqlSessionFactory.openSession(); session.insert("com.like.dao.IUserDao.saveUser", user); session.commit(); session.close(); } ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); User user = new User(); user.setUsername("jack"); user.setSex("男"); user.setAddress("江苏"); user.setBirthday(new Date()); userDao.saveUser(user); ~~~ ## 查询一个 ~~~ SqlSession session = sqlSessionFactory.openSession(); User user = session.selectOne("com.like.dao.IUserDao.findById", id); session.close(); return user; ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); User user = userDao.findById(57); System.out.println(user); ~~~ ## 模糊查询 ~~~ SqlSession session = sqlSessionFactory.openSession(); List<User> users = session.selectList("com.like.dao.IUserDao.findByName", username); session.close(); return users; ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); List<User> users = userDao.findByName("%王%"); System.out.println(users); ~~~ ## 聚合查询 ~~~ SqlSession session = sqlSessionFactory.openSession(); int total = session.selectOne("com.like.dao.IUserDao.findTotal"); session.close(); return total; ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); int total = userDao.findTotal(); System.out.println(total); ~~~ ## 修改 ~~~ SqlSession session = sqlSessionFactory.openSession(); session.update("com.like.dao.IUserDao.updateUser", user); session.commit(); session.close(); ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); User user = new User(); user.setId(58); user.setUsername("richard"); user.setSex("男"); user.setAddress("江苏"); user.setBirthday(new Date()); userDao.updateUser(user); ~~~ ## 删除 ~~~ SqlSession session = sqlSessionFactory.openSession(); session.update("com.like.dao.IUserDao.deletedUser", id); session.commit(); session.close(); ~~~ ~~~ IUserDao userDao = new UserDaoImpl(build); userDao.deletedUser(58); ~~~