Mybatis学习随笔

使用Mybatis步骤

创建Java项目

mysql建表 blog

建立映射导包jar

实体类 blog.java  set/get/tostring/构造函数

  与数据库中表一一对应

 1 package com.hut.bean;
 2 
 3 public class Blog {
 4 
 5     private int id;
 6     private String name;
 7     private String title;
 8     
 9     /*
10      * get/set 
11      */
12     public int getId() {
13         return id;
14     }
15     public void setId(int id) {
16         this.id = id;
17     }
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     public String getTitle() {
25         return title;
26     }
27     public void setTitle(String title) {
28         this.title = title;
29     }
30     
31     /*
32      * 输出
33      */
34     @Override
35     public String toString() {
36         return "blog [id=" + id + ", name=" + name + ", title=" + title + "]";
37     }
38     /*
39      * 构造方法 无参/含参
40      */
41     public Blog(int id, String name, String title) {
42         super();
43         this.id = id;
44         this.name = name;
45         this.title = title;
46     }
47     
48     public Blog() {
49         super();
50         // TODO 自动生成的构造函数存根
51     }
52     
53 }
Blog.class

测试类 test.java  调用blogmapper

配置文件 mybatis-config.xml

  连接数据库

  加载mapper.xml

 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  <typeAliases> <package name = "com.hut.bean"/></typeAliases>
 7   <environments default="development">
 8     <environment id="development">
 9       <transactionManager type="JDBC"/>
10       <dataSource type="POOLED">
11         <property name="driver" value="com.mysql.jdbc.Driver"/>
12         <property name="url" value="jdbc:mysql://Localhost:3306/blog"/>
13         <property name="username" value="root"/>
14         <property name="password" value="admin"/>
15       </dataSource>
16     </environment>
17   </environments>
18   <mappers>
19     <mapper resource="com/hut/dao/BlogMapper.xml"/>
20   </mappers>
21 </configuration>
mybatis-config.xml

映射器接口 blogmapper.java  

 1 package com.hut.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Param;
 6 
 7 import com.hut.bean.Blog;
 8 
 9 public interface BlogMapper {
10     
11     List<Blog> selectname(String name);
12     //模糊查询
13     List<Blog> select_id_name(@Param("id")int id ,@Param("name") String name);
14     //多条件查询
15     void deleteblog(String name);
16     //删除
17     void addblog(Blog blog);
18     //添加
19     void updateblog(Blog blog);
20     //更新
21     List<Blog> selectall();
22     //打印全部blog
23     
24     List<Blog> selectblog(@Param("name")String name , @Param("title")String title);
25     //if 动态sql
26     
27     List<Blog> getBlogByIds(@Param("ids") List<Integer> ids);
28     //foreach
29 }
BlogMapper.java

映射器blogmapper.xml 

  表-实体类关联

  SQL语句-将数据映射为对象

  编写crud方法

    insert/delete/select/update

 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="com.hut.dao.BlogMapper">
 6 <!-- sql语句 -->
 7 
 8   <select id="select_id_name" resultType="blog" >
 9     select * from Blog where id >#{id} and name like concat('%',#{name},'%')
10   </select>
11   
12   <select id="selectname" resultType="blog">
13     select * from Blog where name like '%${name}%'
14   </select>
15   
16     <insert id="addblog" >
17     insert into Blog (id,name,title) values (#{id},#{name},#{title})
18     </insert>
19       
20     <delete id="deleteblog"  >
21         delete from Blog where name = #{name}
22     </delete>
23     
24     <update id="updateblog">
25         update blog set
26         name = #{name},
27         title = #{title}
28         where id = #{id}
29     </update>
30     
31     <select id="selectall" resultType = "blog">
32         select * from blog
33     </select>
34     
35     <select id = "selectblog" resultType = "blog">
36         select * from blog
37         <where>
38         <if test = "name != null">
39         name like '%${name}%'
40         </if>
41         <if test = "title != null">
42         title like '%${title}%'
43         </if>
44         </where>
45     </select>
46     
47     <select id="getBlogByIds" resultType="blog">
48     select * from blog
49     where id in
50       <foreach collection="ids" item="item"
51               open="(" separator="," close=")">
52               #{item}
53       </foreach>
54     </select>
55 </mapper>
BlogMapper.xml
原文地址:https://www.cnblogs.com/djhzzl/p/14076440.html