SSM之Mybatis初体验

环境:

IntelliJ idea

mysql

创建:

1.进行数据库表创建

1 CREATE TABLE `zhao` (
2   `id` int(11) NOT NULL AUTO_INCREMENT,
3   `name` varchar(20) DEFAULT NULL,
4   `gender` varchar(20) DEFAULT NULL,
5   `telephone` varchar(25) DEFAULT NULL,
6   `address` varchar(25) DEFAULT NULL,
7   PRIMARY KEY (`id`)
8 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
View Code

2.使用IDEA创建Maven在创建的同时添加webapp

3.进行依赖添加

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>kmybatisdemo</groupId>
  8     <artifactId>mybatis.zhao</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10     <packaging>war</packaging>
 11 
 12     <name>mybatis.zhao Maven Webapp</name>
 13     <!-- FIXME change it to the project's website -->
 14     <url>http://www.example.com</url>
 15 
 16     <properties>
 17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18         <maven.compiler.source>1.7</maven.compiler.source>
 19         <maven.compiler.target>1.7</maven.compiler.target>
 20     </properties>
 21 
 22     <dependencies>
 23 
 24         <dependency>
 25             <groupId>junit</groupId>
 26             <artifactId>junit</artifactId>
 27             <version>4.3</version>
 28             <scope>test</scope>
 29         </dependency>
 30         <!--创建对应的java项目,引入需要的mybatis需要的jar,以及连接mysql数据库的jar!-->
 31 
 32         <!--asm-3.3.1.jar:操作Javaj字节码的类库-->
 33 
 34         <!--cglib-2.2.2.jar:用来动态集成Java类或实现接口-->
 35 
 36         <!--commons-logging-1.1.1.jar:用于通用日志处理-->
 37 
 38         <!--javassist-3.17.1-GA.jar:分析、编辑和创建Java字节码的类库-->
 39 
 40         <!--log4j-1.2.17.jar:日志系统-->
 41 
 42         <!--slf4j-api-1.7.5.jar:日志系统的封装,对外提供统一的API接口-->
 43 
 44         <!--slf4j-log4j12-1.7.5.jar:slf4j对log4j的对应驱动,完成slf4j绑定log4j-->
 45 
 46         <!--mybatis-3.2.2.jar-->
 47 
 48         <!--mysql-connector-java-5.1.7-bin.jar/3306-->
 49         <!--配置mybatis需要的依赖包-->
 50         <dependency>
 51             <groupId>asm</groupId>
 52             <artifactId>asm</artifactId>
 53             <version>3.3.1</version>
 54         </dependency>
 55 
 56         <dependency>
 57             <groupId>cglib</groupId>
 58             <artifactId>cglib</artifactId>
 59             <version>2.2.2</version>
 60         </dependency>
 61 
 62         <dependency>
 63             <groupId>commons-logging</groupId>
 64             <artifactId>commons-logging</artifactId>
 65             <version>1.1.1</version>
 66         </dependency>
 67         <dependency>
 68             <groupId>org.javassist</groupId>
 69             <artifactId>javassist</artifactId>
 70             <version>3.17.1-GA</version>
 71         </dependency>
 72         <dependency>
 73             <groupId>mysql</groupId>
 74             <artifactId>mysql-connector-java</artifactId>
 75             <version>5.1.6</version>
 76         </dependency>
 77         <dependency>
 78             <groupId>log4j</groupId>
 79             <artifactId>log4j</artifactId>
 80             <version>1.2.7</version>
 81         </dependency>
 82         <dependency>
 83             <groupId>org.slf4j</groupId>
 84             <artifactId>slf4j-api</artifactId>
 85             <version>1.7.5</version>
 86         </dependency>
 87 
 88         <dependency>
 89             <groupId>org.slf4j</groupId>
 90             <artifactId>slf4j-log4j12</artifactId>
 91             <version>1.7.5</version>
 92             <scope>test</scope>
 93         </dependency>
 94         <dependency>
 95             <groupId>org.mybatis</groupId>
 96             <artifactId>mybatis</artifactId>
 97             <version>3.3.0</version>
 98         </dependency>
 99 
100 
101     </dependencies>
102 
103     <build>
104         <finalName>mybatis.zhao</finalName>
105         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
106             <plugins>
107                 <plugin>
108                     <artifactId>maven-clean-plugin</artifactId>
109                     <version>3.1.0</version>
110                 </plugin>
111                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
112                 <plugin>
113                     <artifactId>maven-resources-plugin</artifactId>
114                     <version>3.0.2</version>
115                 </plugin>
116                 <plugin>
117                     <artifactId>maven-compiler-plugin</artifactId>
118                     <version>3.8.0</version>
119                 </plugin>
120                 <plugin>
121                     <artifactId>maven-surefire-plugin</artifactId>
122                     <version>2.22.1</version>
123                 </plugin>
124                 <plugin>
125                     <artifactId>maven-war-plugin</artifactId>
126                     <version>3.2.2</version>
127                 </plugin>
128                 <plugin>
129                     <artifactId>maven-install-plugin</artifactId>
130                     <version>2.5.2</version>
131                 </plugin>
132                 <plugin>
133                     <artifactId>maven-deploy-plugin</artifactId>
134                     <version>2.8.2</version>
135                 </plugin>
136             </plugins>
137         </pluginManagement>
138     </build>
139 </project>
View Code

4.进行实体类编写

 1 package domain;
 2 
 3 /**
 4  * Created by zhao
 5  * 2019/5/11 22:19
 6  */
 7 public class Customer {
 8     private Integer id;
 9     private String name;
10     private String gender;
11     private String telephone;
12     private String address;
13 
14     public Integer getId() {
15         return id;
16     }
17 
18     public void setId(Integer id) {
19         this.id = id;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public String getGender() {
31         return gender;
32     }
33 
34     public void setGender(String gender) {
35         this.gender = gender;
36     }
37 
38     public String getTelephone() {
39         return telephone;
40     }
41 
42     public void setTelephone(String telephone) {
43         this.telephone = telephone;
44     }
45 
46     public String getAddress() {
47         return address;
48     }
49 
50     public void setAddress(String address) {
51         this.address = address;
52     }
53 }
View Code

5.建立Mapper接口

 1 package dao;
 2 
 3 import domain.Customer;
 4 
 5 /**
 6  * Created by zhao
 7  * 2019/5/11 22:26
 8  * 添加一个客户
 9  */
10 public interface CustomerMapper {
11     public  void savecustomer(Customer customer);
12 }
View Code

6.建立SQL映射文件

 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 <!-- 该文件编写mybatis中的mapper接口里面的方法提供对应的sql语句 -->
 6 <mapper namespace="dao.CustomerMapper">
 7 
 8     <!-- 添加客户   参数类型parameterType-->
 9     <insert id="savecustomer" parameterType="domain.Customer">
10         INSERT INTO zhao
11             (
12             NAME, 
13             gender, 
14             telephone, 
15             address
16             )
17             VALUES
18             ( 
19             #{name}, 
20             #{gender}, 
21             #{telephone}, 
22             #{address}
23             )
24     </insert>
25 
26 </mapper>
View Code

7.建立SQLMapconfig.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     <!-- 和spring整合后 environments配置将废除 -->
 7     <environments default="development">
 8         <environment id="development">
 9             <!-- 使用jdbc事务管理 -->
10             <transactionManager type="JDBC" />
11             <!-- 数据库连接池 -->
12             <dataSource type="POOLED">
13                 <property name="driver" value="com.mysql.jdbc.Driver" />
14                 <property name="url"
15                     value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
16                 <property name="username" value="root" />
17                 <property name="password" value="1234" />
18             </dataSource>
19         </environment>
20     </environments>
21     
22     <!-- 查找sql映射文件 -->
23     <mappers>
24         <mapper resource="mapper/CustomerMapper.xml"/>
25     </mappers>
26 </configuration>
View Code

8.写测试类

 1 package test;
 2 
 3 import dao.CustomerMapper;
 4 import domain.Customer;
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 import org.junit.Test;
10 
11 
12 import java.io.IOException;
13 import java.io.InputStream;
14 
15 /**
16  * Created by zhao
17  * 2019/5/11 23:09
18  */
19 public class MybatisTest {
20     @Test
21     public void Test() throws IOException {
22         //1.创建SqlSessionFactoryBuilder
23         SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
24         //2.加载sqlMapConfig.xml文件
25         InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
26 
27         //3.创建SqlSessionFactory
28         SqlSessionFactory factory = builder.build(is);
29         //4.打开SqlSession
30         SqlSession sqlSession = factory.openSession();
31         //5.获取Mapper接口对象
32         CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
33         //6.操作
34         Customer customer = new Customer();
35         customer.setName("赵天");
36         customer.setGender("男");
37         customer.setTelephone("13152081256");
38         customer.setAddress("陕西");
39         customerMapper.savecustomer(customer);
40 
41         //7.提交事务
42         sqlSession.commit();
43         //8.关闭资源
44         sqlSession.close();
45 
46     }
47 }
View Code

9.运行

 

项目结构:

遇到的坑:

1. binding.BindingException: Type interface dao.CustomerMapper is not known to

    CustomerMapper.xml文件中的<mapper namespace="dao.CustomerMapper">写的有问题(这是正确的)

2.找不见sqlMapConfig.xml的异常
解决办法:右键点击SqlMapConfig.xml所在的文件夹(我的是config)------>Build Path------>Use as Source Folder(参考的)。

3.IDEA中创建不了JAVA文件
右键点击所在包 Mark Directory as --->> Sources Root

项目地址

posted @ 2019-05-12 01:12  Angry-rookie  阅读(74)  评论(0)    收藏  举报