mybatis映射配置文件
1.select标签
id: 这条SQL 语句的唯一标识,和接口的方法名一致(必须)p
parameterType:入型参数(非必须)
resultType/resultMap:返回值类型(必须)
userMapper.xml
xml
<!--根据ID查询用户数据-->
<select id="findUserById" parameterType="_int" resultType="com.itheima.pojo.User">
select id,username,birthday,sex,address
from tb_user
where id=#{id}
</select>
userMapper接口
java
//dao层
public interface UserMapper {
/**
*根据给定的ID值查询用户
* @param id
* @return
*/
public User findUserById(int id);
}
测试代码
java
@Test
public void testFindUserById() throws IOException {
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用sqlSession,创建一个代理对象
UserMapper sessionMapper = sqlSession.getMapper(UserMapper.class);
User user = sessionMapper.findUserById(1);
//释放资源
sqlSession.close();
System.out.println(user);
}
2.insert标签
id: 这条SQL 语句的唯一标识,和接口的方法名一致(必须)
说明:#{username},#{birthday},#{sex},#{address}大括号里面的值必须和pojo的实体类和user类的属性名一致,否则会报错
userMapper接口
java
/**
* 添加用户数据
* @param user
* @return 受影响的行数
*/
public int addUser(User user);
userMapper.xml
xml
<!--插入数据-->
<insert id="addUser" parameterType="com.itheima.pojo.User">
insert into tb_user (username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address})
</insert>
测试代码
java
@Test
public void testAddUser() throws IOException {
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
//SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(true); //把手动事务改成自动事务
//利用sqlSession,创建一个代理对象
UserMapper sessionMapper = sqlSession.getMapper(UserMapper.class);
User user = new User(null,"白龙马",new Date(System.currentTimeMillis()),"男","东海");
int rowCount = sessionMapper.addUser(user);
if(rowCount>0){
//sqlSession.commit();//提交事务
System.out.println("插入数据成功");
}
//释放资源
sqlSession.close();
}
3.update标签
id: 这条SQL 语句的唯一标识,和接口的方法名一致(必须)
说明:#{username},#{birthday},#{sex},#{address}大括号里面的值必须和pojo的实体类和user类的属性名一致,否则会报错
userMapper接口
java
/**
* 修改用户数据
* @param user 用户对象
* @return 影响的行数
*/
public int updateUser(User user);
userMapper.xml
xml
<!--修改数据-->
<update id="updateUser" parameterType="com.itheima.pojo.User">
update tb_user
set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
where id=#{id}
</update>
测试代码
java
@Test
public void testUpdateUser() throws IOException {
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true); //把手动事务改成自动事务
//利用sqlSession,创建一个代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//根据ID查询数据
User user = userMapper.findUserById(6);
user.setUsername("小白龙");
user.setAddress("东海1");
int rowCount = userMapper.updateUser(user);
if(rowCount>0){
//sqlSession.commit();//提交事务
System.out.println("修改数据成功");
}
//释放资源
sqlSession.close();
}
4.delete标签
id: 这条SQL 语句的唯一标识,和接口的方法名一致(必须)
userMapper接口
java
/**
* 根据ID删除用户数据
* @param id
* @return
*/
public int deleteUserById(Integer id);
userMapper.xml
xml
<delete id="deleteUserById" parameterType="int">
delete from tb_user
where id=#{id}
</delete>
测试代码
java
@Test
public void testDeleteUserById() throws IOException {
//配置文件路径
String resource = "mybatis-config.xml";
//基于配置文件路径,创建字节输入流
InputStream is = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用SqlSessionFactory工厂类,创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true); //把手动事务改成自动事务
//利用sqlSession,创建一个代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int rowCount = userMapper.deleteUserById(7);
if(rowCount>0){
//sqlSession.commit();//提交事务
System.out.println("删除数据成功");
}
//释放资源
sqlSession.close();
}
Mybatis工具类的编写
java
package com.itheima.util;
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 MybatisUtil {
//私有化构造方法
private MybatisUtil(){}
//SqlSessionFactory工厂类对象
private static SqlSessionFactory sqlSessionFactory=null;
//随着类的加载,仅执行一次
static {
try {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//创建SqlSessionFactory工厂类
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
//静态方法返回SqlSession对象
public static SqlSession openSession(){
//使用SqlSessionFactory工厂类,创建SqlSession对象
return sqlSessionFactory.openSession(); //默认:手动事务
}
public static SqlSession openSession(boolean flag){
//使用SqlSessionFactory工厂类,创建SqlSession对象
return sqlSessionFactory.openSession(flag);//根据参数指定事务
}
//静态方法关闭sqlSession
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.close();
}
}
}