一、idea中创建maven工程
选择file–>new–>project,选择maven和与自己相搭配的jdk**
创建好项目组地址,方便结构清晰
选择自己的项目路径,点击完成。
二、idea中搭建mybatis框架
三、idea中完成单表增删改查操作
二、配置maven
创建对应的java项目,引入需要的mybatis需要的jar,以及连接数据库的jar包(我用的数据库为mysql)
<?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.file</groupId>
<artifactId>MyBatisTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
三、创建数据库
创建简单的小型测试类数据库
四、创建实体类与接口
1、在项目中src–>main–>java目录下创建包以及测试类
package com.Test;
public class User {
public Integer id;
public String name;
public String password;
public User() {
super();
}
public User(Integer id,String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +"id=" + id + ","
+ " name='" + name + '\'' + ", "
+ "password='" + password + '\'' + '}';
}
}
2、在包下新建一个接口(UserMapper)
package com.Test;
import java.util.List;
public interface UserMapper {
//查询所有信息
public List<User> queryAll();
//添加
public void addUser(User user);
//修改
public boolean updateUser(User user);
//根据id删除信息
public void deleteUserById(int id);
}
五、配置文件
1、在resources新建一个properties文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/db_test?characterEncoding=UTF-8
jdbc.username = root
jdbc.password =123456
2、在resources新建一个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">
<!--
mybatis可以使用properties来引入外部properties配置文件的内容
resource:引入类路径下的资源
-->
<properties resource="db.properties"></properties>
<!-- environments:环境,mybatis可以设置多种环境;default指定使用某种环境,可以达到快速切换环境
environment:配置一个具体的环境信息;必须有两个标签;id代表当前环境的唯一标识
type:事务管理器的类型;JDBC(JdbcTransactionFactory)MANAGED(ManagedTransactionFactory)
自定义事务管理器:实现TransactionFactory接口,type指定为全类名
dataSource:数据源
type:数据源UNPOOLED(UnpooledDataSourceFactory)
POOLED(PooledDataSourceFactory)
JNDI(JndiDataSourceFactory)
自定义数据源:实现DataSourceFactory接口,type是全类名
-->
<environments default="db_test">
<environment id="db_test">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- databaseIdProvider:支持多数据库厂商的;
type="DB_VENDOR":VendorDatabaseIdProvider
作用就是得到数据库厂商的标识(驱动getDatabaseIDProductName()),mybatis就能根据数据库厂商标识来执行不同的sql;
-->
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
</databaseIdProvider>
<mappers>
<mapper resource="UserMapper.xml"/> <!-- sql映射文件路径 -->
</mappers>
3、配置SQL映射文件, 在resources新建一个xml文件(UserMapper.xml)
<?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">
<!--
namespace :名称空间;指定接口的全类名
id:唯一标识(建议用接口中对应的方法名命名)
resultType:返回值类型
-->
<select id="queryAll" resultType="com.Test.User">
select * from tb_user
</select>
<!--
自增主键值的获取,mybatis也是利用statement.getGeneratedKeys();
useGeneratedKeys="true";使用自增获取主键值策略
keyProperty:指定对应的主键值,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
-->
<insert id="addUser" useGeneratedKeys="true" keyProperty="id">
insert into tb_user(name,password) values(#{name},#{password})
</insert>
<!-- 根据name修改password的值 -->
<update id="updateUser">
update tb_user set password=#{password} where name=#{name}
</update>
<delete id="deleteUserById">
delete from tb_user where id=#{id}
</delete>
六、测试
在 src / test / java 下新建一个类(Test)
1、测试添加
import com.Test.User;
import com.Test.UserMapper;
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;
import java.util.List;
public class Test {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@org.junit.Test
public void demo() throws IOException {
SqlSessionFactory sessionFactory = getSqlSessionFactory();
SqlSession openSession = sessionFactory.openSession();
try {
UserMapper mapper = openSession.getMapper(UserMapper.class);
User user = new User(null, "樊", "123456");
mapper.addUser(user);
System.out.println(user);
openSession.commit(); //执行增删改是需手动提交数据
} finally {
openSession.close();
}
}
}
2、测试修改(在Test中try后修改即可)
UserMapper mapper = openSession.getMapper(UserMapper.class);
User user = new User(null, "樊", "666");
mapper.updateUser(user);
openSession.commit();
3、测试查询
List list = openSession.selectList("com.Test.UserMapper.queryAll");
for (User user:list) {
System.out.println(user);
4、测试删除
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.deleteUserById(2);
openSession.commit(); //执行增删改是需手动提交数据