mapper接口本質就是原來的Dao接口,知識爲了方便咱們的書寫,一個mapper接口對應一個mapper映射文件java
將UserMapper改名爲UserDao,說明本質mysql
修改對應的mapper映射文件的namespace屬性,對應上接口。sql
1 package com.like.dao; 2 3 import com.like.pojo.User; 4 5 import java.util.List; 6 7 public interface UserDao{ 8 //獲取所有的用戶 9 List<User>selectUser(); 10 11 //根據id查找用戶 12 User electUserById(int id); 13 14 //添加一個用戶 15 int addUser(User user); 16 17 //刪除用戶 18 int deleteUserByID(int id); 19 20 //修改用戶 21 int updateUser(User user); 22 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!--mapper標籤的namespace對應的Mapper接口的類,報名+類名--> 6 <mapper namespace="com.like.dao.UserDao"> 7 <!--select標籤的id對應映射接口的方法名字 resultType:返回結果的類型 中間就編寫sql語句--> 8 <select id="selectUser" resultType="com.like.pojo.User"> 9 select*from user 10 </select> 11 <select id="selectUserById" resultType="com.like.pojo.User"> 12 select*from user where id =#{id} 13 </select> 14 15 <!--咱們須要接受一個自定義的對象(引用隊形) 須要設置parameterTpye,爲參數類型 16 接收這個對象的值,直接使用#{對象字段名} 17 --> 18 <insert id="addUser" parameterType="com.like.pojo.User"> 19 inset into user(id,name,pwd)values(#{id},#{name},#{pwd}) 20 </insert> 21 22 <delete id="deleteUserByID" parameterType="int"> 23 delete from user where id =#{id} 24 </delete> 25 26 <update id="updateUser" parameterType="com.like.pojo.User"> 27 updata user set name=#{name},pwd=#{pwd}where id=#{id} 28 </update> 29 </mapper>
1 package com.like.dao; 2 3 import com.like.dao.UserDao; 4 import com.like.pojo.User; 5 import dom.like.utils.MyBatisUtils; 6 import org.apache.ibatis.session.SqlSession; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.junit.Test; 9 import java.util.List; 10 11 public class UserMapperTest{ 12 13 @Test 14 public void selectUser(){ 15 //1.拿到sqlSessionFactory對象 16 SqlSessionFactory sqlSessionFactory=MyBatisUtils.getSqlSessionFactory(); 17 //2.經過sqlSessionFactory對象openSession()建立一個sqlSession. 18 SqlSession sqlSession =sqlSessionFactory.openSession(); 19 //3.經過sqlSession得到mapper對象,參數爲映射文件對應的接口類的class隊形 20 UserDao mapper =sqlSession.getMapper(UserDao.class); 21 //4.經過mapper對象來執行操做; 22 List<User>users =mapper.selectUser(); 23 //得到結果集 24 for (User user : users){ 25 System.out.println(user); 26 } 27 sqlSession.close();//關閉sqlSession 28 29 } 30 @Test 31 public void addUser(){ 32 User user = new User(4,"qinjiang","123456"); 33 34 SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory(); 35 SqlSession sqlSession = sqlSessionFactory.openSession(); 36 UserDao mapper = sqlSession.getMapper(UserDao.class); 37 38 int i = mapper.addUser(user); 39 40 //沒有插入成功的緣由:沒有提交事務; 41 sqlSession.commit(); //提交事務 42 sqlSession.close(); //關閉sqlSession 43 44 if (i>0){ 45 System.out.println("插入成功!"); 46 } 47 48 } 49 50 @Test 51 public void deleteUserByID(){ 52 SqlSession sqlSession = MyBatisUtils.getSqlSessionFactory().openSession(); 53 UserDao mapper = sqlSession.getMapper(UserDao.class); 54 int i = mapper.deleteUserByID(4); 55 56 //記得提交事務,不然刪除不成功! 57 sqlSession.commit();//提交事務 58 sqlSession.close();//關閉 59 60 if (i>0){ 61 System.out.println(i); 62 } 63 } 64 65 @Test 66 public void updateUser(){ 67 SqlSession sqlSession = MyBatisUtils.getSqlSessionFactory().openSession(); 68 UserDao mapper = sqlSession.getMapper(UserDao.class); 69 User user = new User(1,"李柯","like"); 70 mapper.updateUser(user); 71 72 sqlSession.commit(); 73 sqlSession.close(); 74 75 } 76 }
注意點總結數據庫
增改刪須要提交事務!apache
若是出現亂碼,如今sql中進行測試,sql沒問題,就檢查配置文件session
jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding="utf-8"
參數問題mybatis
若是時基本數據類型,能夠省略,但建議寫上 引用類型必須寫指定的 包名+類名。app
mybatis-config文件解析dom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration> 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 configuration 配置 6 properties 配置文件 7 settiongs mybatis設置 8 typeHandlers 爲java類起別名 9 objectFactory 對象工廠 10 plugins 插件 11 environments 環境 12 transactionManager :事務管理 13 dataSource :數據源 14 mappers 映射器
優化配置文件測試
建立一個database.properties
1 driver = com.mysql.jdbc.Driver 2 url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8 3 username = root 4 password = 123456
在mybatis核心配置文件中引入properties配置文件,並用${}表達式引入其中的值
1 <configuration> 2 3 <!--配置文件修改--> 4 <properties resource="database.properties"/> 5 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="${driver}"/> 11 <property name="url" value="${url}"/> 12 <property name="username" value="${username}"/> 13 <property name="password" value="${password}"/> 14 </dataSource> 15 </environment> 16 </environments> 17 18 <mappers> 19 <mapper resource="com/kuang/dao/userMapper.xml"/> 20 </mappers> 21 22 </configuration>
優化別名
注意點,位置必須正確
1 <!--配置別名--> 2 <typeAliases> 3 4 <!--配置指定類的別名--> 5 <typeAlias type="com.like.pojo.User" alias="User"/> 6 7 <!-- 8 能夠爲一個包的全部類指定別名,這個別名爲類名 9 com.like.pojo.User - > User 10 com.like.pojo.Student - > Student 11 12 --> 13 <package name="com.like.pojo"/> 14 15 </typeAliases>
優化完畢後咱們在mapper映射文件中就能夠使用別名了
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <!--mapper標籤的namespace對應Mapper接口的類,包名+類名--> 7 <mapper namespace="com.like.dao.UserDao"> 8 9 <!--select標籤的id對應映射接口的方法名字 resultType:返回結果的類型 中間就編寫sql語句--> 10 <select id="selectUser" resultType="User"> 11 select * from user 12 </select> 13 14 <select id="selectUserById" resultType="User"> 15 select * from user where id = #{id} 16 </select> 17 18 <!-- 19 咱們須要接受一個自定義的對象(引用對象),須要設置parameterType,爲參數類型 20 接受這個對象的值,直接使用 #{對象字段名} 21 --> 22 <insert id="addUser" parameterType="User"> 23 insert into user(id ,name, pwd) values (#{id},#{name},#{pwd}) 24 </insert> 25 26 <delete id="deleteUserByID" parameterType="int"> 27 delete from user where id = #{id} 28 </delete> 29 30 <update id="updateUser" parameterType="User"> 31 update user set name =#{name},pwd = #{pwd} where id = #{id} 32 </update> 33 34 </mapper>
環境配置
1 <environments default="development"> 2 <environment id="development"> 3 <transactionManager type="JDBC"/> 4 <dataSource type="POOLED"> 5 <property name="driver" value="${driver}"/> 6 <property name="url" value="${url}"/> 7 <property name="username" value="${username}"/> 8 <property name="password" value="${password}"/> 9 </dataSource> 10 </environment> 11 </environments>
映射器
1,resource(推薦)
2,url
3,class
4.package
<!-- 使用相對於類路徑的資源引用 --> <mappers> <mapper resource="org/mybatis/builder/AuthorMapper.xml"/> <mapper resource="org/mybatis/builder/BlogMapper.xml"/> <mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers>
字段名:數據庫 id name pwd
屬性名:實體類 id name password
咱們修改完畢後測試運行一下:
分析:
select*from user 等價於 select id ,name ,pwd from user;
mybatis會根據數據庫的字段名去找對應的實體類的屬性名,(他會將全部的列名轉換爲小寫,而後去找實體類中對應的set方法,set方法後面的字段就對應數據庫的字段名,若是不一楊就返回null)