本文共 8010 字,大约阅读时间需要 26 分钟。
参考文献:
需要写dao接口和dao实现类。
package cn.itcast.mybatis.dao;import cn.itcast.mybatis.po.Employee;public interface UserDao { public Employee findUserById(int id) throws Exception; public void insertUser(Employee employee) throws Exception; public void deleteUser(int id) throws Exception;}
package cn.itcast.mybatis.dao;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import cn.itcast.mybatis.po.Employee;public class UserDaoImpl implements UserDao{ private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public Employee findUserById(int id) throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); Employee employee = sqlSession.selectOne("test.findUserById",id); sqlSession.close(); return employee; } @Override public void insertUser(Employee employee) throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); sqlSession.insert("test.insertUser",employee); 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(); }}
package cn.itcast.mybatis.dao;import static org.junit.Assert.*;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import cn.itcast.mybatis.po.Employee;public class UserDaoImplTest { private SqlSessionFactory sqlSessionFactory; @Before public void setUp() throws Exception{ String resource="SqlMapConfig.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test() throws Exception { UserDao userDao = new UserDaoImpl(sqlSessionFactory); Employee employee = userDao.findUserById(3); System.out.println(employee); }}
缺点:
接口实现类方法中存在大量模板方法,设想能否将这些代码提取出来。
另外调用SqlSession方法时,将Statement的id硬编码了。 调用SqlSession方法时,传入的变量,由于SqlSession方法使用泛型,即使变量类型传入错误,在编译阶段也不报错,不利于开发。
UserMapper.xml
) ①在mapper.xml中namespace等于mapper接口地址 ②mapper.java接口中的方法名和mapper.xml中的Statement的id一致 ③mapper.java接口中的方法输入参数类型和mapper.xml中Statement的parameterType指定类型一致 ④mapper.java接口中的方法返回值类型和mapper.xml中Statement的resultType指定的类型一致UserMapper.java
)package cn.itcast.mybatis.mapper;import cn.itcast.mybatis.po.Employee;public interface UserMapper { public Employee findUserById(int id) throws Exception; public java.util.ListfindUserByName(String name) throws Exception; public void insertUser(Employee employee ) throws Exception; public void deleteUser(int id) throws Exception;}
SqlMapConfig.xml
加载映射文件package cn.itcast.mybatis.mapper;import static org.junit.Assert.*;import java.io.InputStream;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import cn.itcast.mybatis.po.Employee;public class UserMapperTest {private SqlSessionFactory sqlSessionFactory; @Before public void setUp() throws Exception{ String resource="SqlMapConfig.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test() throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); Employee employee =userMapper.findUserById(2); System.out.println(employee); } @Test public void test1() throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); java.util.Listlist =userMapper.findUserByName("zhao"); sqlSession.close(); System.out.println(list); }}
SqlMapConfig.xml
db.properties
(和SqlMapConfig.xml
在同一目录下)jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8jdbc.username=rootjdbc.password=
UserMapper.xml
(定义用户信息综合查询,查询条件复杂)UserQueryVo.java
(包装对象)package cn.itcast.mybatis.po;public class UserQueryVo { private EmployeeCustom employeeCustom; public EmployeeCustom getEmployeeCustom() { return employeeCustom; } public void setEmployeeCustom(EmployeeCustom employeeCustom) { this.employeeCustom = employeeCustom; }}
EmployeeCustom.java
package cn.itcast.mybatis.po;public class EmployeeCustom extends Employee{ }
测试文件
@Test public void test1() throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //创建包装对象,设置查询条件 UserQueryVo userQueryVo= new UserQueryVo(); EmployeeCustom employeeCustom=new EmployeeCustom(); employeeCustom.setSex("nv"); employeeCustom.setUsername("zhou"); userQueryVo.setEmployeeCustom(employeeCustom); //调用userMapper的方法 java.util.Listlist =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }
UserMapper.xml
(判断传入的sex和username是否为空,若是空则不进行拼接)@Test public void test1() throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserQueryVo userQueryVo= new UserQueryVo(); EmployeeCustom employeeCustom=new EmployeeCustom(); //employeeCustom.setSex("nv");//注释此句后,查询语句中没有sex这个条件 employeeCustom.setUsername("zhou"); userQueryVo.setEmployeeCustom(employeeCustom); java.util.Listlist =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }
** 进行select * from users where id=1 or id=10 or id=11 ;**
UserMapper.xml
中添加id=#{user_id}
@Test public void test1() throws Exception { SqlSession sqlSession =sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserQueryVo userQueryVo= new UserQueryVo(); EmployeeCustom employeeCustom=new EmployeeCustom(); //employeeCustom.setSex("nv"); employeeCustom.setUsername("zhou"); java.util.Listids =new ArrayList (); ids.add(1); ids.add(10); ids.add(14); userQueryVo.setIds(ids); userQueryVo.setEmployeeCustom(employeeCustom); java.util.List list =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }
转载地址:http://zvqtx.baihongyu.com/