一、准备环境(我的demo数据如下)
1、创建数据。
CREATE DATABASE my_db;
CREATE TABLE user(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
parentId INT(11),
sonId INT(11),
name VARCHAR(255),
birthday DATE
)DEFAULT CHARSET=utf8;
2、加入 jar包 maven pom.xml :
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
</dependencies>
二、目录结构如下:
1、创建 User.java 对象
package com.zll.TestMyBatis.bean;
import java.util.Date;
public class User {
private int id;
private int parentId;
private int sonId;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public int getSonId() {
return sonId;
}
public void setSonId(int sonId) {
this.sonId = sonId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2、创建 User.xml 映射文件。
注意: namespace=“hello” 和 insert id=“world”
这个hello 和 world 关键字后面会用到,这里你先跟着写。
<?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">
<!--会面会根据关键字 hello 和 world的来查找对象的方法-->
<mapper namespace="hello">
<!-- 增加 要使用 insert标签 -->
<insert id="world" parameterType="com.zll.TestMyBatis.bean.User">
insert into user(parentId,sonId,name,birthday) value(#{parentId},#{sonId},#{name},#{birthday})
</insert>
</mapper>
1、先创建 mybatis 配置文件:mybatis-config.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/my_db?characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 这里改成自己User.xml的路径 -->
<mapper resource="com/zll/TestMyBatis/bean/User.xml"/>
</mappers>
</configuration>
4、使用junit 开始测试:
public class AppTest extends TestCase {
public void insertUser() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
User user = new User();
user.setName("小明");
user.setBirthday(new Date());
//这里调用的就是 User.xml 映射的 namespace 和 id了,insert内部是根据这两个关键字来查找映射路径的,(当然起名字最好起的有意义)
session.insert("hello.world", user);
session.commit();
session.close();
}
}
一条简单的数据插入成功。