普通模式
自定义接口,接口实现类。
思考:需要sqlSessionFactory,生产sqlSession。
UserDao:
package dao;import java.util.List;import domain.User;public interface UserDao { //根据Id查询用户 public User findUserByID(Integer id); //根据用户名进行模糊查询 public ListfindUserByUsername(String username);}
UserDaoImpl:
package dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import dao.UserDao;import domain.User;public class UserDaoImpl implements UserDao{ //namespace private String ns="test."; private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory){ this.sqlSessionFactory=sqlSessionFactory; } public User findUserByID(Integer id) { SqlSession sqlSession=sqlSessionFactory.openSession(); User user = sqlSession.selectOne(ns+"findUserByID",id); return user; } public ListfindUserByUsername(String username) { SqlSession sqlSession=sqlSessionFactory.openSession(); List list=sqlSession.selectList(ns+"findUserByUsername", username); return list; }}
Main3:
package test;import static org.hamcrest.CoreMatchers.nullValue;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.List;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 dao.UserDao;import dao.impl.UserDaoImpl;import domain.User;public class Main3 { SqlSessionFactory sqlSessionFactory=null; @Before public void beforeConf() throws IOException{ String resources="sqlMapConfig.xml"; InputStream inputStream=Resources.getResourceAsStream(resources); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } //普通dao开发模式 @Test public void commonDao(){ UserDao userDao=new UserDaoImpl(sqlSessionFactory); //根据Id查询用户 User user1 = userDao.findUserByID(16); System.out.println("user1:::"+user1); //根据名字模糊查询用户 Listlist = userDao.findUserByUsername("张"); System.out.println(list); }}
接口代理开发模式
自需要定义接口。
开发约定:
l 映射文件namespace必须是接口全类路径名。
l 映射文件的Statement的id必须和接口的方法名一致。
sqlMapConfig.xml:
User.xml:
UserMapper:
package dao;import java.util.List;import domain.User;public interface UserMapper { //根据Id查询用户 public User findUserByID(Integer id); //根据用户名进行模糊查询 public ListfindUserByUsername(String username);}
Main1:
package test;import java.io.IOException;import java.io.InputStream;import java.util.List;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 dao.UserMapper;import domain.User;public class Main1 { SqlSessionFactory sqlSessionFactory=null; @Before public void beforeConf() throws IOException{ String resources = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resources); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } //接口代理开发模式 @Test public void proxyInterface(){ SqlSession sqlSession=sqlSessionFactory.openSession(); //获取代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //根据Id查询 User user = userMapper.findUserByID(24); System.out.println(user); //根据用户名进行模糊查询 Listusers=userMapper.findUserByUsername("张"); System.out.println(users); }}
SqlMapConfig.xml配置文件