码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis Map的简单使用

时间:2020-04-04 14:20:44      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:puts   app   导入   try   rac   system   username   工具类   pen   

1 //第一新建一个TeacherMapper接口
2 public interface TeacherMapper {
3         //使用map修改
4         int updateTeacherMap(Map<String,Object> map);
5         //使用map插入
6         int addTeacherMap(Map<String,Object> map);
7 }
 1 //第二建立对象TeacherMapper对应的xml
 2 <?xml version="1.0" encoding="UTF-8" ?>
 3 <!DOCTYPE mapper
 4         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 5         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 6     //TeacherMapper对应的是上面接口的名字,通过这个名字 **xml** 于接口进行绑定
 7 <mapper namespace="com.rzk.mapper.TeacherMapper">
 8     <!--map修改-->
 9     <update id="updateTeacherMap" parameterType="map">
10        update mybatis.teacher set name=#{name} where id=#{id}
11     </update>
12     <!--map插入-->
13     <insert id="addTeacherMap" parameterType="map">
14         insert into mybatis.teacher(id, name) values (#{tid},#{tname})
15     </insert>
16 </mapper>
 1 //第三建pojo
 2 public class Teacher {
 3     private int id;
 4     private String name;
 5     public Teacher() {
 6     }
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public Teacher(int id, String name) {
20         this.id = id;
21         this.name = name;
22     }
23     @Override
24     public String toString() {
25         return "Teacher{" +
26                 "id=" + id +
27                 ", name=‘" + name + \‘ +
28                 };
29     }
30 }

 

 1 //第四mybatis建工具类
 2 public class MybatisUtils {
 3     private static SqlSessionFactory sqlSessionFactory;
 4     static{
 5         try {
 6             String resources="mybatis-config.xml";
 7             InputStream inputStream = Resources.getResourceAsStream(resources);
 8             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 9         } catch (IOException e) {
10             e.printStackTrace();
11         }
12     }
13     public static SqlSession getSqlSessionFactory() {
14         return sqlSessionFactory.openSession();
15     }
16 }

 

 1 //编写config.xml
 2 <?xml version="1.0" encoding="UTF-8" ?>
 3 <!DOCTYPE configuration
 4         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 5         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 6 <!--configguration核心配置文件-->
 7 <configuration>
 8     <!--导入外部配置文件-->
 9     <properties resource="db.properties"/>
10     <!--标准日志工厂实现-->
11     <!--   <settings>
12            <setting name="logImpl" value="STDOUT_LOGGING"/>
13        </settings>-->
14     <settings>
15         <setting name="logImpl" value="LOG4J"/>
16     </settings>
17     <!--给实体类起别名-->
18     <typeAliases>
19         <package name="com.rzk.pojo" />
20     </typeAliases>
21     <environments default="development">
22         <environment id="development">
23             <transactionManager type="JDBC"/>
24             <dataSource type="POOLED">
25                 <property name="driver" value="${driver}"/>
26                 <property name="url" value="${url}"/>
27                 <property name="username" value="${username}"/>
28                 <property name="password" value="${password}"/>
29             </dataSource>
30         </environment>
31     </environments>
32     <mappers>
33 <!--        <mapper resource="com/rzk/dao/StudentMapper.xml"/>-->
34         <mapper resource="com/rzk/mapper/TeacherMapper.xml"/>
35 <!--        <mapper class="com.rzk.mapper.Teacher"/>-->
36         <mapper class="com.rzk.mapper.StudentMapper"/>
37     </mappers>
38 </configuration>

 

 1 测试类
 2   /*map修改*/
 3     @Test
 4     public void updateTeacherMap(){
 5         SqlSession sqlSession = MybatisUtils.getSqlSessionFactory();
 6       //通过反射获取接口里面的东西
 7         TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
 8         Map<String, Object> map = new HashMap<>();
 9         map.put("id",1);
10         map.put("name","精致的猪猪女孩");
11         //增删改查都要进行提交
12         int i = mapper.updateTeacherMap(map);
13         if (i>0){
14             System.out.println("修改成功");
15             sqlSession.commit();
16         }
17         sqlSession.close();
18     }
19   /*map的插入*/
20     @Test
21     public void addTeacher1(){
22         SqlSession sqlSession = MybatisUtils.getSqlSessionFactory();
23         TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
24         Map<String, Object> map = new HashMap<String,Object>();
25         map.put("tid",3);
26         map.put("tname","李大强");
27         mapper.addTeacherMap(map);
28         sqlSession.commit();
29         sqlSession.close();
30     }
31 
32 底层使用的还是jdbc的东西

技术图片

 

mybatis Map的简单使用

标签:puts   app   导入   try   rac   system   username   工具类   pen   

原文地址:https://www.cnblogs.com/rzkwz/p/12631567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!