普通模式
自定义接口,接口实现类。
思考:需要sqlSessionFactory,生产sqlSession。
UserDao:
package dao; import java.util.List; import domain.User; public interface UserDao { //根据Id查询用户 public User findUserByID(Integer id); //根据用户名进行模糊查询 public List<User> findUserByUsername(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 List<User> findUserByUsername(String username) { SqlSession sqlSession=sqlSessionFactory.openSession(); List<User> 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); //根据名字模糊查询用户 List<User> list = userDao.findUserByUsername("张"); System.out.println(list); } }
接口代理开发模式
自需要定义接口。
开发约定:
l 映射文件namespace必须是接口全类路径名。
l 映射文件的Statement的id必须和接口的方法名一致。
sqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 配置文件开始,所有配置文件都必须在configuration --> <configuration> <!-- 可以配置多个运行环境,但是每个 SqlSessionFactory 实例只能选择一个运行环境 一、development:开发模式 二、work:工作模式--> <environments default="development"> <!--id属性必须和上面的default一样 --> <environment id="development"> <!-- mybatis事务管理器 ,由jdbc管理--> <transactionManager type="JDBC"/> <!-- mybatis连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis01"/> <property name="username" value="root"/> <property name="password" value="moujinling321"/> </dataSource> </environment> </environments> <!-- 引入外部映射文件 --> <mappers> <mapper resource="sqlMap/User.xml"/> </mappers> </configuration>
User.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- MyBatis映射文件的开始标签 ,所有sql语句全部封装在mapper里面 namespace:命名空间 *隔离不同的映射文件,映射文件的唯一标识,可以任意命名 *在接口代理开发中,namespace具有特殊含义,不能任意命名 namespace需要写接口的全路径 --> <mapper namespace="dao.UserMapper"> <!-- statement的id必须和接口中的方法名相同 --> <select id="findUserByID" parameterType="int" resultType="domain.User"> select * from user where id=#{id} </select> <select id="findUserByUsername" resultType="domain.User" parameterType="string"> select * from user where username like ‘%${value}%‘ </select> </mapper>
UserMapper:
package dao; import java.util.List; import domain.User; public interface UserMapper { //根据Id查询用户 public User findUserByID(Integer id); //根据用户名进行模糊查询 public List<User> findUserByUsername(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); //根据用户名进行模糊查询 List<User> users=userMapper.findUserByUsername("张"); System.out.println(users); } }