這裡是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html 接下來做到了簡單的增刪改查:http://www.cnblogs.com/xuyiqing/p/8601506.html 但是發現代碼重覆過多等問題 接下來整合併實現DAO開發: 一:原 ...
這裡是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html
接下來做到了簡單的增刪改查:http://www.cnblogs.com/xuyiqing/p/8601506.html
但是發現代碼重覆過多等問題
接下來整合併實現DAO開發:
一:原始DAO開發:
package dao; import pojo.User; public interface UserDao { public User selectUserById(Integer id); }
package dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import pojo.User; public class UserDaoImpl implements UserDao { //註入 private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } //通過用戶ID查詢一個用戶 public User selectUserById(Integer id){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession.selectOne("test.findUserById", id); } //通過用戶名稱模糊查詢 public List<User> selectUserByUsername(Integer id){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession.selectList("test.findUserById", id); } }
測試類:
package junit; 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 dao.UserDao; import dao.UserDaoImpl; import pojo.User; public class DaoTest { public SqlSessionFactory sqlSessionFactory; @Before public void before() throws Exception { String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } @Test public void testDao() throws Exception { UserDao userDao = new UserDaoImpl(sqlSessionFactory); User user = userDao.selectUserById(10); System.out.println(user); } }
但是還是發現代碼重覆、浪費資源等問題:
於是想到Mapper動態代理開發:
package mapper; import pojo.User; public interface UserMapper { //遵循四個原則 //介面 方法名 == User.xml 中 id 名 //返回值類型 與 Mapper.xml文件中返回值類型要一致 //方法的入參類型 與Mapper.xml中入參的類型要一致 //命名空間 綁定此介面 //這裡如果返回的是一個對象,則調用selectOne方法 //如果是List,則調用selectlist方法 public User findUserById(Integer id); }
UserMapper.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"> <!-- 寫Sql語句 --> <mapper namespace="mapper.UserMapper"> <!-- 通過ID查詢一個用戶 --> <select id="findUserById" parameterType="Integer" resultType="pojo.User"> select * from user where id = #{v} </select> <!-- //根據用戶名稱模糊查詢用戶列表 #{} select * from user where id = ? 占位符 ? == '五' ${} select * from user where username like '%五%' 字元串拼接 --> <select id="findUserByUsername" parameterType="String" resultType="pojo.User"> select * from user where username like "%"#{haha}"%" </select> <!-- 添加用戶 --> <insert id="insertUser" parameterType="pojo.User"> <selectKey keyProperty="id" resultType="Integer" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into user (username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex}) </insert> <!-- 更新 --> <update id="updateUserById" parameterType="pojo.User"> update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} where id = #{id} </update> <!-- 刪除 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where id = #{vvvvv} </delete> </mapper>
主配置文件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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="xuyiqing" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
測試類:
package junit; 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.Test; import mapper.UserMapper; import pojo.User; public class MybatisMapperTest { @Test public void testMapper() throws Exception { String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); SqlSession sqlSession = sqlSessionFactory.openSession(); //SqlSession自動為介面生成一個實現類 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById(10); System.out.println(user); } }
通常情況下,建議使用Mapper動態代理開發
配置上還可以完善下:
主配置文件:
<?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> <properties resource="jdbc.properties"/> <typeAliases> <package name="pojo"/> </typeAliases> <!-- 和spring整合後 environments配置將廢除 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事務管理 --> <transactionManager type="JDBC" /> <!-- 資料庫連接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!--Mapper.xml:寫Sql語句的文件的位置 --> <mappers> <!-- 這種方式要求包和對應xml文件在一個包中 --> <package name="mapper"/> </mappers> </configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=xuyiqing
在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"> <!-- 寫Sql語句 --> <mapper namespace="mapper.UserMapper"> <!-- 通過ID查詢一個用戶 --> <select id="findUserById" parameterType="Integer" resultType="User"> select * from user where id = #{v} </select> <!-- //根據用戶名稱模糊查詢用戶列表 #{} select * from user where id = ? 占位符 ? == '五' ${} select * from user where username like '%五%' 字元串拼接 --> <select id="findUserByUsername" parameterType="String" resultType="User"> select * from user where username like "%"#{haha}"%" </select> <!-- 添加用戶 --> <insert id="insertUser" parameterType="pojo.User"> <selectKey keyProperty="id" resultType="Integer" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into user (username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex}) </insert> <!-- 更新 --> <update id="updateUserById" parameterType="User"> update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} where id = #{id} </update> <!-- 刪除 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where id = #{vvvvv} </delete> </mapper>