Mybatis的CRUD的简单操作
User类
package com.code_g.domain;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
核心配置文件
<?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:///test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
mapping文件配置
<?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="userMapper">
<select id="findAll" resultType="com.code_g.domain.User">
select * from user
</select>
<insert id="save" parameterType="com.code_g.domain.User">
insert into user values (#{id},#{username},#{password})
</insert>
<update id="update" parameterType="com.code_g.domain.User">
update user set username=#{username},password=#{password} where id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
</mapper>
测试类
package com.code_g.test;
import com.code_g.domain.User;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
@Test
//查询操作
public void test1() throws IOException {
//读取配置文件
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建sqlsession工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
//获取sqlsession对象
SqlSession sqlSession = sessionFactory.openSession();
//执行sql
List<User> list = sqlSession.selectList("userMapper.findAll");
System.out.println(list);
//释放资源
sqlSession.close();
}
@Test
//插入操作
public void test2() throws IOException {
User user = new User();
user.setUsername("Tom");
user.setPassword("123");
//读取配置文件
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建sqlsession工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
//获取sqlsession对象
SqlSession sqlSession = sessionFactory.openSession();
//执行sql
sqlSession.insert("userMapper.save",user);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
@Test
//更新操作
public void test3() throws IOException {
User user = new User();
user.setId(11);
user.setUsername("Jerry");
user.setPassword("456");
//读取配置文件
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建sqlsession工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
//获取sqlsession对象
SqlSession sqlSession = sessionFactory.openSession();
//执行sql
sqlSession.update("userMapper.update",user);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
@Test
//删除操作
public void test4() throws IOException {
//读取配置文件
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建sqlsession工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resource);
//获取sqlsession对象
SqlSession sqlSession = sessionFactory.openSession();
//执行sql
sqlSession.delete("userMapper.delete",12);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
}