mybatis的入门

mybatis的前身是ibatis,其配置和使用步骤为:

配置步骤:

1、  配置全局配置文件

设置环境(事务、数据源)

管理映射文件XxxMapper.xml

2、  配置映射文件

Mapper标签:用来配置不同的statement

Namespace属性:用来配置名称空间,来区分不同的映射文件中的statement

编写crud的statement:select 、update 、insert 、delete

         Select:专门用于查询的statement,可以编写查询语句

         Id属性:该statement的唯一标识,一般用来被引用

         resultType:结果集类型

         paramterType:参数类型,可以省略,一般都省略

使用步骤:

1、获取全局配置文件的输入流,加载全局配置文件

2、通过全局配置文件的输入流创建SqlSessionFactory

3、通过SqlSessionFactory获取SqlSession

4、通过SqlSession操作crud

5、关闭SqlSession,释放资源。

全局配置文件如下:

 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     <!--配置环境,注册mysql驱动-->
 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/mybatis"/>
13                 <property name="username" value="root"/>
14                 <property name="password" value="root"/>
15             </dataSource>
16         </environment>
17     </environments>
18     <!--将映射xml交给全局配置文件托管-->
19     <mappers>
20         <mapper resource="UserMapper.xml"/>
21     </mappers>
22 </configuration>

映射文件如下:

 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="UserMapper">
 6     <!--一定要注意resultType后面的事全限定名-->
 7     <!--
 8     select标签:sql语句的声明
 9     id:sql语句的唯一标识
10     resultType:返回的是什么类型
11     -->
12     <select id="queryUserById" resultType="cn.itcast.pojo.User">
13         /*sql语句*/
14         select *,user_name as userName from tb_user where id =#{id}
15     </select>
16 </mapper>

bean类:

  1 package cn.itcast.pojo;
  2 
  3 import java.util.Date;
  4 
  5 public class User {
  6 
  7     private Long id;
  8 
  9     // 用户名
 10     private String userName;
 11 
 12     // 密码
 13     private String password;
 14 
 15     // 姓名
 16     private String name;
 17 
 18     // 年龄
 19     private Integer age;
 20 
 21     // 性别,1男性,2女性
 22     private Integer sex;
 23 
 24     // 出生日期
 25     private Date birthday;
 26 
 27     // 创建时间
 28     private Date created;
 29 
 30     // 更新时间
 31     private Date updated;
 32 
 33     public Long getId() {
 34         return id;
 35     }
 36 
 37     public void setId(Long id) {
 38         this.id = id;
 39     }
 40 
 41     public String getUserName() {
 42         return userName;
 43     }
 44 
 45     public void setUserName(String userName) {
 46         this.userName = userName;
 47     }
 48 
 49     public String getPassword() {
 50         return password;
 51     }
 52 
 53     public void setPassword(String password) {
 54         this.password = password;
 55     }
 56 
 57     public String getName() {
 58         return name;
 59     }
 60 
 61     public void setName(String name) {
 62         this.name = name;
 63     }
 64 
 65     public Integer getAge() {
 66         return age;
 67     }
 68 
 69     public void setAge(Integer age) {
 70         this.age = age;
 71     }
 72 
 73     public Integer getSex() {
 74         return sex;
 75     }
 76 
 77     public void setSex(Integer sex) {
 78         this.sex = sex;
 79     }
 80 
 81     public Date getBirthday() {
 82         return birthday;
 83     }
 84 
 85     public void setBirthday(Date birthday) {
 86         this.birthday = birthday;
 87     }
 88 
 89     public Date getCreated() {
 90         return created;
 91     }
 92 
 93     public void setCreated(Date created) {
 94         this.created = created;
 95     }
 96 
 97     public Date getUpdated() {
 98         return updated;
 99     }
100 
101     public void setUpdated(Date updated) {
102         this.updated = updated;
103     }
104 
105     @Override
106     public String toString() {
107         return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
108                 + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
109                 + ", updated=" + updated + "]";
110     }
111 
112 }

测试类:

 1 package cn.itcast.mybatis;
 2 
 3 import cn.itcast.pojo.User;
 4 import org.apache.ibatis.io.Resources;
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 8 
 9 import java.io.IOException;
10 import java.io.InputStream;
11 
12 /**
13  * @author o_0sky
14  * @date 2019/2/14 20:02
15  */
16 public class MybatisTest {
17     public static void main(String[] args) throws IOException {
18         //获取全局配置输入流
19         String resource = "mybatis-config.xml";
20         InputStream inputStream = Resources.getResourceAsStream(resource);
21         //加载全局配置文件
22         SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
23         //获取sqlSession
24         SqlSession sqlSession = sessionFactory.openSession();
25         /**
26          * 第一个参数:namespace.id
27          * 第二个参数:sql语句传递的参数
28          */
29         User user = sqlSession.selectOne("UserMapper.queryUserById", 1L);
30         System.out.println(user);
31     }
32 }

这是基础的mybatis配置及使用,后续还会进行更新

原文地址:https://www.cnblogs.com/linsky/p/10389949.html