标签:根据 host sel 获取 ssi cal 获得 like 接受
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 }
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&useUnicode=true&characterEncoding="utf-8"
参数问题
如果时基本数据类型,可以省略,但建议写上 引用类型必须写指定的 包名+类名。
标签:根据 host sel 获取 ssi cal 获得 like 接受
原文地址:https://www.cnblogs.com/like-lry/p/11320291.html