本次全部学习内容:MyBatisLearning
在UserDao.java接口中:
public interface UserDao { //根据id查询用户信息 public User findUserById(int id) throws Exception; //添加用户信息 public void insertUser(User user) throws Exception; //删除用户信息 public void deleteUser(int id) throws Exception; }
//UserDao的实现类 public class UserDaoImp implements UserDao { //需要向dao实现类里面注入SqlSessionFactory //通过构造器注入 private SqlSessionFactory SqlSessionFactory; public UserDaoImp(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 void insertUser(User user) throws Exception { SqlSession sqlSession = SqlSessionFactory.openSession(); sqlSession.insert("test.addUser", 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(); } }
在testUserDaoImp.java中:
public class testUserDaoImp { private SqlSessionFactory SqlSessionFactory; public SqlSessionFactory getSqlSessionFactory() throws IOException{ //配置文件的 String resource = "SqlMapConfig.xml"; //得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工程 SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return SqlSessionFactory; } //根据id查询用户信息 @Test public void findUserById() throws Exception{ SqlSessionFactory SqlSessionFactory = getSqlSessionFactory(); UserDao userdao = new UserDaoImp(SqlSessionFactory); User user = userdao.findUserById(1); System.out.println(user); } //添加用户信息 }
控制台:
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 2054574951. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a765367] DEBUG [main] - ==> Preparing: select * from user where id=? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1
//添加用户信息 @Test public void testAddUser() throws Exception{ SqlSessionFactory SqlSessionFactory = getSqlSessionFactory(); UserDao userdao = new UserDaoImp(SqlSessionFactory); User user = new User(); user.setUsername("Ma"); user.setSex(1); user.setAddress("安徽"); userdao.insertUser(user); }
控制台:
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 22429093. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1563da5] DEBUG [main] - ==> Preparing: insert into user(id,username,birthday,sex,address) value(?,?,?,?,?) DEBUG [main] - ==> Parameters: 0(Integer), Ma(String), null, 1(Integer), 安徽(String) DEBUG [main] - <== Updates: 1 DEBUG [main] - ==> Preparing: SELECT LAST_INSERT_ID() DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 1
//删除用户信息 @Test public void testDelete() throws Exception{ SqlSessionFactory SqlSessionFactory = getSqlSessionFactory(); UserDao userdao = new UserDaoImp(SqlSessionFactory); userdao.deleteUser(29); }
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 22429093. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1563da5] DEBUG [main] - ==> Preparing: delete from user where id=? DEBUG [main] - ==> Parameters: 29(Integer) DEBUG [main] - <== Updates: 1 DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1563da5]
<mappers>
<mapper resource="sqlmap/User.xml"/>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
在UserMapper.java接口中:
//根据id查询用户信息 public User findUserById(int id) throws Exception;
<mapper namespace="com.MrChengs.mapper.UserMapper"> <!-- 根据id查询 --> <!-- public User findUserById(int id) throws Exception; --> <select id="findUserById" parameterType="int" resultType="com.MrChengs.po.User"> select * from user where id=#{id} </select> </mapper>
在测试类testUserMapper.java类中
private SqlSessionFactory SqlSessionFactory; public SqlSessionFactory getSqlSessionFactory() throws IOException{ //配置文件的 String resource = "SqlMapConfig.xml"; //得到配置文件流 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工程 SqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return SqlSessionFactory; } @Test public void testfindUserById() throws Exception{ //得到sqlsession SqlSession sqlSession = getSqlSessionFactory().openSession(); //创建UserMapper的代理对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //调用userMapper的方法 User user = mapper.findUserById(27); System.out.println(user); sqlSession.close(); }
实现成功:
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 352359770. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1500955a] DEBUG [main] - ==> Preparing: select * from user where id=? DEBUG [main] - ==> Parameters: 27(Integer) DEBUG [main] - <== Total: 1 User [id=27, username=MrChegns, birthday=Fri Oct 05 00:00:00 CST 2018, sex=2, address=北京]
//根据用户名查询(模糊查询) public List<User> findByName(String name) throws Exception;
<!-- 根据用户名查询 --> <select id="findByName" parameterType="java.lang.String" resultType="com.MrChengs.po.User" > select * from user where username Like ‘%${value}%‘ </select>
测试类:
@Test public void testfindByName() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); //创建UserMapper的代理对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //调用userMapper的方法 List<User> users = mapper.findByName("小明"); for(User user:users){ System.out.println(user); } sqlSession.close(); }
查询成功:
DEBUG [main] - ==> Preparing: select * from user where username Like ‘%小明%‘ DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 3 User [id=16, username=张小明, birthday=null, sex=1, address=河南郑州] User [id=22, username=陈小明, birthday=null, sex=1, address=河南郑州] User [id=25, username=陈小明, birthday=null, sex=1, address=河南郑州]
原文:https://www.cnblogs.com/Mrchengs/p/9750076.html