MyBatis初次环境配置

简单介绍

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)

流程

功能


实例

导包

核心包
核心包

依赖包
依赖包

当然还要有数据库驱动包

写xml配置

项目结构如下:
项目结构

添加SqlMapConfig.xml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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/db_pwc" />
<property name="username" value="pwc" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/pwc/pojo/User.xml" />
</mappers>
</configuration>

添加POJO类User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.pwc.pojo;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
}
}

添加POJO类User的映射文件User.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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="com.pwc.dao.mapper.UserMapper">
<select id="findById" parameterType="int" resultType="com.pwc.pojo.User">
SELECT * FROM User WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.pwc.pojo.User">
INSERT INTO User(name,age) VALUES(#{name},#{age})
</insert>
<delete id="deleteById" parameterType="int">
DELETE * FROM User WHERE id=#{id}
</delete>
<select id="findByAge" parameterType="int" resultType="com.pwc.pojo.User">
SELECT * FROM User WHERE age=#{age}
</select>
</mapper>

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.pwc.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.pwc.pojo.User;
public class Test {
public static void main(String[] args) {
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(Test.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
SqlSession session = factory.openSession();
User user=session.selectOne("findById", 1);
System.out.println(user.toString());
}
}

测试结果如下:

1
User [id=1, name=pwc, age=18]