2.Mybatis兩種開發模式
阿新 • • 發佈:2018-03-20
throws users pan except family RM Coding apache 不同
普通模式
自定義接口,接口實現類。
思考:需要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); } }
2.Mybatis兩種開發模式