package com.itheima.dao.impl; import com.itheima.dao.IUserDao; import com.itheima.domain.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; /** * @author 黑马程序员 * @Company http://www.ithiema.com */ public class UserDaoImpl implements IUserDao { private SqlSessionFactory factory; public UserDaoImpl(SqlSessionFactory factory){ this.factory = factory; } @Override public ListfindAll() { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 List users = session.selectList("com.itheima.dao.IUserDao.findAll");//参数就是能获取配置信息的key //3.释放资源 session.close(); return users; } @Override public void saveUser(User user) { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用方法实现保存 session.insert("com.itheima.dao.IUserDao.saveUser",user); //3.提交事务 session.commit(); //4.释放资源 session.close(); } @Override public void updateUser(User user) { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用方法实现更新 session.update("com.itheima.dao.IUserDao.updateUser",user); //3.提交事务 session.commit(); //4.释放资源 session.close(); } @Override public void deleteUser(Integer userId) { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用方法实现更新 session.update("com.itheima.dao.IUserDao.deleteUser",userId); //3.提交事务 session.commit(); //4.释放资源 session.close(); } @Override public User findById(Integer userId) { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用SqlSession中的方法,实现查询一个 User user = session.selectOne("com.itheima.dao.IUserDao.findById",userId); //3.释放资源 session.close(); return user; } @Override public List findByName(String username) { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用SqlSession中的方法,实现查询列表 List users = session.selectList("com.itheima.dao.IUserDao.findByName",username); //3.释放资源 session.close(); return users; } @Override public int findTotal() { //1.根据factory获取SqlSession对象 SqlSession session = factory.openSession(); //2.调用SqlSession中的方法,实现查询一个 Integer count = session.selectOne("com.itheima.dao.IUserDao.findTotal"); //3.释放资源 session.close(); return count; } }
mapper
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">select last_insert_id(); insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday});update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id} delete from user where id = #{uid}
sqlconfig
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">default="mysql">
test
package com.itheima.test; import com.itheima.dao.IUserDao; import com.itheima.dao.impl.UserDaoImpl; import com.itheima.domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.InputStream; import java.util.Date; import java.util.List; /** * @author 黑马程序员 * @Company http://www.ithiema.com * * 测试mybatis的crud操作 */ public class MybatisTest { private InputStream in; private IUserDao userDao; @Before//用于在测试方法执行之前执行 public void init()throws Exception{ //1.读取配置文件,生成字节输入流 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.获取SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); //3.使用工厂对象,创建dao对象 userDao = new UserDaoImpl(factory); } @After//用于在测试方法执行之后执行 public void destroy()throws Exception{ //6.释放资源 in.close(); } /** * 测试查询所有 */ @Test public void testFindAll(){ //5.执行查询所有方法 Listusers = userDao.findAll(); for(User user : users){ System.out.println(user); } } /** * 测试保存操作 */ @Test public void testSave(){ User user = new User(); user.setUsername("dao impl user"); user.setAddress("北京市顺义区"); user.setSex("男"); user.setBirthday(new Date()); System.out.println("保存操作之前:"+user); //5.执行保存方法 userDao.saveUser(user); System.out.println("保存操作之后:"+user); } /** * 测试更新操作 */ @Test public void testUpdate(){ User user = new User(); user.setId(50); user.setUsername("userdaoimpl update user"); user.setAddress("北京市顺义区"); user.setSex("女"); user.setBirthday(new Date()); //5.执行保存方法 userDao.updateUser(user); } /** * 测试删除操作 */ @Test public void testDelete(){ //5.执行删除方法 userDao.deleteUser(54); } /** * 测试删除操作 */ @Test public void testFindOne(){ //5.执行查询一个方法 User user = userDao.findById(50); System.out.println(user); } /** * 测试模糊查询操作 */ @Test public void testFindByName(){ //5.执行查询一个方法 List users = userDao.findByName("%王%"); for(User user : users){ System.out.println(user); } } /** * 测试查询总记录条数 */ @Test public void testFindTotal(){ //5.执行查询一个方法 int count = userDao.findTotal(); System.out.println(count); } }
pom
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.itheima day02_eesy_02mybatsiDAO 1.0-SNAPSHOT jar org.mybatis mybatis 3.4.5 mysql mysql-connector-java 5.1.6 log4j log4j 1.2.12 junit junit 4.10