步骤:首先在pom里导入依赖—>先写工具类—>环境准备resource里的mybatis-config.xml —>写实体类—>写接口—>写接口对应的xml配置文件—>写test文件
pom文件依赖:
<?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.zsp</groupId>
<artifactId>mybaties-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybaties-01</module>
<module>mybaties-test</module>
</modules>
<dependencies>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybaties-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
先写工具类:
package com.zsp.utils;
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 MyUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
然后工具类上面会有"mybatis-config.xml"指引我们去写个接口的配置类:
环境准备resource里的mybatis-config.xml这里面是导入了我们的数据库连接:
mapper可以先不写,需要绑定接口的配置类了在写:
<?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/mybaties?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zsp/dao/UserMapper.xml"/>
</mappers>
</configuration>
写实体类就是pojo类:
package com.zsp.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
这是接口类
package com.zsp.dao;
import com.zsp.pojo.User;
import java.util.List;
public interface UserMapper {
// 查找功能
List<User> queryAllUser();
//增加功能
int addUser(User user);
// 删除功能
int delUserById(int id);
// 修改功能
int updateUser(User user);
}
接口的配置类里面放入增删改查:
<?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.zsp.dao.UserMapper">
<select id="queryAllUser" resultType="com.zsp.pojo.User">
select * from mybaties.user;
</select>
<insert id="addUser" parameterType="com.zsp.pojo.User">
insert into mybaties.user (id,name,pwd)values(#{id},#{name},#{pwd});
</insert>
<delete id="delUserById" parameterType="com.zsp.pojo.User">
delete from mybaties.user where id =#{id};
</delete>
<update id="updateUser" parameterType="com.zsp.pojo.User">
update mybaties.user set name =#{name} ,pwd=#{pwd} where id =#{id};
</update>
</mapper>
测试类:
package com.zsp.dao;
import com.zsp.pojo.User;
import com.zsp.utils.MyUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class TestUserMapper {
@Test
public void test(){
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.queryAllUser();
for (User user:list)
{
System.out.println(user);
}
}
@Test
public void addUser(){
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int num = mapper.addUser(new User(5, "zsp5", "123456"));
if (num>0)
{
System.out.println("执行成功");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void delUserById(){
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.delUserById(5);
if (i>0)
{
System.out.println("执行成功");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUserById(){
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int zsp1 = mapper.updateUser(new User(1, "zsp1", "123124"));
if (zsp1>0)
{
System.out.println("执行成功");
}
sqlSession.commit();
sqlSession.close();
}
}