一.工程前的准备工作
- 下载jdk(省略)
- 下载idea,并指定下载的jdk为源(省略)
- 下载maven,并配置到idea中(省略)
二.创建工程
- 新建JAVA工程,在JAVA工程中新建自己的的模块
目录结构如下
- 把模块转换成maven工程后,在pom.xml中添加如下配置信息
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.it18zhang</groupId>
<artifactId>mybatisdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
- 新建mybatis-config.xml中,在resource中的同包名的目录下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
数据库的操作
-
在数据库中建同名库和表
mysql>create database mybatis ; mysql>use mybatis ; mysql>create table users(id int primary key auto_increment , name varchar(20) ,age int) ; mysql>desc users ;
测试链接 在java下创建App.java
package com.it18zhang.mybatisdemo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
*
*/
public class App {
public static void main(String[] args) {
try {
//指定配置文件的路径(类路径)
String resource = "mybatis-config.xml";
//加载文件
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂Builder,相当于连接池
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
//通过sf开启会话,相当于打开连接。
SqlSession s = sf.openSession();
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
编写Mapper文件
创建User类,和users对应 public class User { private Integer id ; private String name ; private int age ; //get/set }
-
创建UserMapper.xml,存放在resources/目录下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 定义名字空间 --> <mapper namespace="users"> <!-- 定义insert语句 --> <insert id="insert"> insert into users(name,age) values(#{name},#{age}) </insert> </mapper>
-
在resources/mybatis-config.xml文件中引入mapper的xml文件.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- *****引入映射文件(新增部分)***** --> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
-
编写单元测试,实现插入
[test/com.it18zhang.mybatis.test.TestCRUD.java] /** * insert */ @Test public void insert() throws Exception { //指定配置文件的路径(类路径) String resource = "mybatis-config.xml"; //加载文件 InputStream inputStream = Resources.getResourceAsStream(resource); //创建会话工厂Builder,相当于连接池 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream); //通过sf开启会话,相当于打开连接。 SqlSession s = sf.openSession(); User u = new User(); u.setName("jerry"); u.setAge(2); s.insert("users.insert", u); s.commit(); s.close(); }