1.mybatis-config配置文件
<?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="unpooled">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dzqc/mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
2.实体类用户
public class User {
int id;
String name;
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(){
}
}
3.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">
<mapper namespace="com.dzqc.mapper.UserMapper">
<select id="queryAllUsers" resultType="com.dzqc.entity.User">
select * from myuser
</select>
<!-- useGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键;keyProperty="id"指定把获取到的主键值注入到User的id属性 -->
<insert id="insertUser" parameterType="com.dzqc.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into myuser(name,age) values (#{name},#{age})
</insert>
<update id="updateUser" parameterType="com.dzqc.entity.User">
update myuser set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from myuser where id =#{id}
</delete>
<select id="selectOneUser" parameterType="int" resultType="com.dzqc.entity.User">
select * from myuser where id=#{id}
</select>
</mapper>
4.测试类
package com.dzqc.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.dzqc.entity.User;
public class Test1 {
@Test
public void testSelect(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Test1.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
List<User> lst = session.selectList("com.dzqc.mapper.UserMapper.queryAllUsers");
System.out.println(lst);
}
@Test
public void testInsert(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Test1.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
User user = new User(5,"hello",23);
session.insert("com.dzqc.mapper.UserMapper.insertUser", user);
session.commit();
}
@Test
public void testUpdateUser(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Test1.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
User user = new User(5,"hello2",24);
session.update("com.dzqc.mapper.UserMapper.updateUser", user);
session.commit();
}
@Test
public void testDelete(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Test1.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
session.delete("com.dzqc.mapper.UserMapper.deleteUser", 5);
session.commit();
}
@Test
public void testSelectOne(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Test1.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
User user = session.selectOne("com.dzqc.mapper.UserMapper.selectOneUser", 3);
System.out.println(user);
}
}
接口实现方式
package com.dzqc.mapper;
import java.util.List;
import com.dzqc.entity.User;
/**
* 接口名称和映射文件名一致
* 方法名跟相应的操作标签id值保持一致
* 返回值类型和resultType保持一致
* 参数类型和parameterType保持一致
*/
public interface UserMapper {
public List<User> queryAllUsers();
public void insertUser(User user);
public void updateUser(User user);
public void deleteUser(int id);
public User selectOneUser(int id);
}
测试类
package com.dzqc.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.dzqc.entity.User;
import com.dzqc.mapper.UserMapper;
public class TestInterface {
@Test
public void testSelect(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestInterface.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//先获取接口对象 再调用接口中的相应方法
UserMapper mapper=session.getMapper(UserMapper.class);
List<User> lst =mapper.queryAllUsers();
System.out.println(lst);
}
@Test
public void testInsert(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestInterface.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//先获取接口对象 再调用接口中的相应方法
UserMapper mapper=session.getMapper(UserMapper.class);
User user = new User(7,"nihao",56);
mapper.insertUser(user);
session.commit();
}
@Test
public void testUpdateUser(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestInterface.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//先获取接口对象 再调用接口中的相应方法
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = new User(4,"好",24);
userMapper.updateUser(user);
session.commit();
}
@Test
public void testDelete(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestInterface.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//先获取接口对象 再调用接口中的相应方法
UserMapper userMapper = session.getMapper(UserMapper.class);
userMapper.deleteUser(6);
session.commit();
}
@Test
public void testSelectOne(){
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = TestInterface.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//先获取接口对象 再调用接口中的相应方法
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectOneUser(4);
System.out.println(user);
}
}