文件结构
2.1基于XML的增、删、改、查
2.1.1定义sql映射文件
<?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.config.userMapper">
<!-- 根据id得到一个user对象 -->
<!-- 参数类型:parameterType resultType:结果集类型(全类名)-->
<select id="getUser" parameterType="int"
resultType="com.entity.User">
select * from user where id=#{id}
</select>
<!-- 插入操作 -->
<insert id="addUser" parameterType="com.entity.User">
insert into user(name,age) values(#{name},#{age})
</insert>
<!-- 删除操作 -->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
<!-- 更新操作 -->
<update id="updateUser" parameterType="com.entity.User">
update user set name=#{name},age=#{age} where id=#{id}
</update>
<!-- 查询所有 -->
<select id="getAllUsers" resultType="com.entity.User">
select * from user
</select>
</mapper>
2.1.2在config.xml文件中注册这个映射文件
<mappers>
<mapper resource="com/config/userMapper.xml"/>
</mappers>
2.1.3定义一个MybatisUtil类,主要用于获取SqlSession
package com.util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil {
public static SqlSession getSession() throws IOException
{
String resource = "config.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//openSession的参数设置为true表示自动提交
SqlSession session=sessionFactory.openSession(true);
return session;
}
}
2.1.4定义相应的测试方法
package com.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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 com.entity.User;
import com.util.MybatisUtil;
public class Test1 {
@Test
public void getUser() throws IOException
{
String resource = "config.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中 sql 的 sqlSession
SqlSession session = sessionFactory.openSession();
//映射 sql 的标识字符串
String statement = "com.config.userMapper.getUser";
//执行查询返回一个唯一 user 对象的 sql
User user = session.selectOne(statement, 15);
session.close();
System.out.println(user);
}
//测试插入
@Test
public void addUser() throws IOException
{
SqlSession session=MybatisUtil.getSession();
String statement="com.config.userMapper.addUser";
//insert表示有几天记录发生了变化
int insert=session.insert(statement, new User(-1,"王五",29));
session.close();
System.out.println(insert);
}
//测试删除
@Test
public void deleteUser() throws IOException
{
SqlSession session=MybatisUtil.getSession();
String statement="com.config.userMapper.deleteUser";
int delete=session.delete(statement, 15);
session.close();
System.out.println(delete);
}
//测试更新
@Test
public void updateUser() throws IOException
{
SqlSession session=MybatisUtil.getSession();
String statement="com.config.userMapper.updateUser";
int update=session.update(statement, new User(16,"马六",34));
session.close();
System.out.println(update);
}
//测试获得整个表
@Test
public void getAllUsers() throws IOException
{
SqlSession session=MybatisUtil.getSession();
String statement="com.config.userMapper.getAllUsers";
List<User> list=session.selectList(statement);
for(User user:list)
{
System.out.println(user);
}
session.close();
}
}
2.2基于注解的增、删、改、查
2.2.1定义sql映射接口
package com.inter;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.entity.User;
public interface UserMapper {
@Insert("insert into user(name,age) values(#{name},#{age})")
public int add(User user);
@Delete("delete from user where id=#{id}")
public int deleteById(int id);
@Update("update user set name=#{name},age=#{age} where id=#{id}")
public int update(User user);
@Select("select * from user where id=#{id}")
public User getById(int id);
@Select("select * from user")
public List<User> getAll();
}
2.2.2在config.xml中注册这个映射接口
<mappers>
<mapper class="com.inter.UserMapper"/>
</mappers>
2.2.3编写测试方法
package com.test;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import com.entity.User;
import com.inter.UserMapper;
import com.util.MybatisUtil;
public class Test2 {
//测试添加
@Test
public void testAdd() throws IOException
{
SqlSession session=MybatisUtil.getSession();
UserMapper mapper=session.getMapper(UserMapper.class);
int add=mapper.add(new User(-1,"王五",12));
session.close();
System.out.println(add);
}
//测试删除
@Test
public void testDelete() throws IOException
{
SqlSession session=MybatisUtil.getSession();
UserMapper mapper=session.getMapper(UserMapper.class);
int delete=mapper.deleteById(17);
session.close();
System.out.println(delete);
}
//测试更新
@Test
public void testUpdate() throws IOException
{
SqlSession session=MybatisUtil.getSession();
UserMapper mapper=session.getMapper(UserMapper.class);
int update=mapper.update(new User(16,"王五",17));
session.close();
System.out.println(update);
}
//测试查询
@Test
public void testGetUsers() throws IOException
{
SqlSession session=MybatisUtil.getSession();
UserMapper mapper=session.getMapper(UserMapper.class);
List<User> list=mapper.getAll();
for(User user:list)
{
System.out.println(user);
}
session.close();
}
}