1、基础回顾

1.1、引入jar包

 1 <dependency>
 2     <groupId>org.mybatis</groupId>
 3     <artifactId>mybatis</artifactId>
 4     <version>3.2.8</version>
 5 </dependency>
 6 
 7 <dependency>
 8     <groupId>mysql</groupId>
 9     <artifactId>mysql-connector-java</artifactId>
10     <version>5.1.32</version>
11 </dependency>

 

1.2、 创建数据库表

 1 CREATE TABLE PERSON(
 2  id INT(11) PRIMARY KEY AUTO_INCREMENT COMMENT '人员编号',
 3  `name` NVARCHAR(50) NOT NULL COMMENT '姓名',
 4  sex NVARCHAR(50) NOT NULL COMMENT '性别',
 5  age INT(20) NOT NULL COMMENT '年龄',
 6  rank NVARCHAR(50) NOT NULL COMMENT '等级',
 7  department NVARCHAR(100) NOT NULL COMMENT '所在部门'
 8 )COMMENT '人员信息表';
 9 
10 INSERT INTO PERSON(id,name,sex,age,rank,department) VALUES(1,'张三','',25,'1级','开发部');

 1.3、JDBC基础代码回顾

 1 package com.ft.dao;
 2 
 3 
 4 
 5 import java.sql.DriverManager;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 import com.mysql.jdbc.Connection;
10 import com.mysql.jdbc.PreparedStatement;
11 
12 public class JDBCTest {
13     public static void main(String[] args) {
14         Connection conn = null;
15         PreparedStatement pst = null;
16         ResultSet rs = null;
17         try {
18             Class.forName("com.mysql.jdbc.Driver");
19             String url = "jdbc:mysql://127.0.0.1:3306/ssmdemo";
20             String user = "root";
21             String password = "root";
22             conn = (Connection) DriverManager.getConnection(url, user, password);
23             String sql  = "select * from person";
24             pst = (PreparedStatement) conn.prepareStatement(sql);
25             rs = pst.executeQuery();
26             while(rs.next()) {
27                 System.out.print(rs.getString("id"));
28                 System.out.print(rs.getString("name"));
29                 System.out.print(rs.getString("sex"));
30                 System.out.print(rs.getString("age"));
31                 System.out.print(rs.getString("rank"));
32                 System.out.println(rs.getString("department"));
33             }
34             
35         }catch (Exception e) {
36             // TODO: handle exception
37         }
38         finally {
39             if(rs != null) {
40                 try {
41                     rs.close();
42                     pst.close();
43                     conn.close();
44                 } catch (SQLException e) {
45                     // TODO Auto-generated catch block
46                     e.printStackTrace();
47                 }
48             }
49         }
50         
51     }
52     
53     
54 }

 

1.4、JDBC确定分析

 

2、MyBatis介绍

 

 3、MyBatis整体架构

 

 4、MyBatis快速入门

4.1、全局配置文件(mybatis-config.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 <!-- 根标签 -->
 6 <configuration>
 7 <properties>
 8     <property name="driver" value="com.mysql.jdbc.Driver"/>
 9     <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis-110?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true"/>
10     <property name="username" value="root"/>
11         <property name="password" value="123456"/>
12    </properties>
13 
14    <!-- 环境,可以配置多个,default:指定采用哪个环境 -->
15    <environments default="test">
16       <!-- id:唯一标识 -->
17       <environment id="test">
18          <!-- 事务管理器,JDBC类型的事务管理器 -->
19          <transactionManager type="JDBC" />
20          <!-- 数据源,池类型的数据源 -->
21          <dataSource type="POOLED">
22             <property name="driver" value="com.mysql.jdbc.Driver" />
23             <property name="url" value="jdbc:mysql://127.0.0.1:3306/ssmdemo" />
24             <property name="username" value="root" />
25             <property name="password" value="root" />
26          </dataSource>
27       </environment>
28       <environment id="development">
29          <!-- 事务管理器,JDBC类型的事务管理器 -->
30          <transactionManager type="JDBC" />
31          <!-- 数据源,池类型的数据源 -->
32          <dataSource type="POOLED">
33             <property name="driver" value="${driver}" /> <!-- 配置了properties,所以可以直接引用 -->
34             <property name="url" value="${url}" />
35             <property name="username" value="${username}" />
36             <property name="password" value="${password}" />
37          </dataSource>
38       </environment>
39    </environments>
40    <mappers>
41            <mapper resource="mapper/MyMapper.xml"/>
42    </mappers>
43    
44   </configuration>

 

 

 

4.2、 配置Map.xml(MyMapper.xml)

 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:命名空间,随便写,一般保证命名空间唯一 -->
 6 <mapper namespace="MyMapper">
 7    <!-- statement,内容:sql语句。id:唯一标识,随便写,在同一个命名空间下保持唯一
 8       resultType:sql语句查询结果集的封装类型,tb_user即为数据库中的表
 9     -->
10    <select id="selectperson" resultType="com.ft.entity.Person">
11       select * from person where id = #{id}
12    </select>
13 </mapper>

 

4.3、目录结构

 

 

 4.4、javaBean类

 1 package com.ft.entity;
 2 
 3 public class Person {
 4     private int id;
 5     private String name;
 6     private String sex;
 7     private int age;
 8     private String rank;
 9     private String department;
10     public int getId() {
11         return id;
12     }
13     public void setId(int id) {
14         this.id = id;
15     }
16     public String getName() {
17         return name;
18     }
19     public void setName(String name) {
20         this.name = name;
21     }
22     public String getSex() {
23         return sex;
24     }
25     public void setSex(String sex) {
26         this.sex = sex;
27     }
28     public int getAge() {
29         return age;
30     }
31     public void setAge(int age) {
32         this.age = age;
33     }
34     public String getRank() {
35         return rank;
36     }
37     public void setRank(String rank) {
38         this.rank = rank;
39     }
40     public String getDepartment() {
41         return department;
42     }
43     public void setDepartment(String department) {
44         this.department = department;
45     }
46     @Override
47     public String toString() {
48         return "Person [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", rank=" + rank
49                 + ", department=" + department + "]";
50     }
51     public Person(int id, String name, String sex, int age, String rank, String department) {
52         super();
53         this.id = id;
54         this.name = name;
55         this.sex = sex;
56         this.age = age;
57         this.rank = rank;
58         this.department = department;
59     }
60     public Person() {
61         super();
62     }
63     
64 }

 4.5、测试代码

 

 1 package com.ft.dao;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 import com.ft.entity.Person;
12 
13 public class Mybatistest {
14     public static void main(String[] args) throws IOException {
15         String resource = "mybatis-config.xml";
16         //读取配置文件
17         InputStream input = Resources.getResourceAsStream(resource);
18         //构建sqlSessionFactory
19         SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(input);
20         //获取sqlSession
21         SqlSession session = sessionFactory.openSession();
22         try {
23              Person per = session.selectOne("MyMapper.selectperson", 1);
24              System.out.println(per);
25         } finally {
26             // TODO: handle finally clause
27             session.close();
28         }
29         
30         
31     }
32 }

 

posted on 2020-11-27 20:29  二十二画生的执著  阅读(126)  评论(0编辑  收藏  举报