增删改查操作

mapper接口本质就是原来的Dao接口,知识为了方便我们的书写,一个mapper接口对应一个mapper映射文件

将UserMapper更名为UserDao,说明本质

修改对应的mapper映射文件的namespace属性,对应上接口。

编写接口

 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  }

对应的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 <!--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 }

注意点总结

增改删需要提交事务!

如果出现乱码,现在sql中进行测试,sql没问题,就检查配置文件

jdbc:mysql://localhost:3306/mybatis?
useSSL=true&amp;useUnicode=true&amp;characterEncoding="utf-8"

参数问题

如果时基本数据类型,可以省略,但建议写上    引用类型必须写指定的  包名+类名。

 配置文件的详解

mybatis-config文件解析

 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&amp;useUnicode=true&amp;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)

posted on 2019-08-08 11:49  影子谜  阅读(119)  评论(0编辑  收藏  举报