一、项目创建
详见链接,本文仅展示增删改查的写法
https://blog.csdn.net/XD_XZD/article/details/85318260
二、映射文件
<?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.mybatis.demo1.mapper.UserMapper" >
<!-- 通过name查询一条记录 -->
<select id="findUserByName" resultType="com.mybatisdemo.demo1.entity.User" parameterType="java.lang.String">
SELECT NAME,AGE,SEX FROM USER WHERE NAME = #{name}
</select>
<!-- 添加一个用户 -->
<insert id="createUser" parameterType="com.mybatisdemo.demo1.entity.User">
INSERT INTO USER (NAME, AGE, SEX) VALUES (#{name}, #{age}, #{sex})
</insert>
<!-- 删除一个用户 -->
<delete id="deleteUser" parameterType="java.lang.String">
DELETE FROM USER WHERE NAME = #{name}
</delete>
<!-- 修改一个用户 -->
<update id="updateUser" parameterType="com.mybatisdemo.demo1.entity.User">
UPDATE USER SET AGE = #{age}, SEX = #{sex} WHERE NAME = #{name}
</update>
<!-- 查询所有用户 -->
<select id="selectAllUser" resultType="com.mybatisdemo.demo1.entity.User">
SELECT NAME,AGE,SEX FROM USER
</select>
</mapper>
三、测试类
package com.mybatisdemo.demo1;
import com.mybatisdemo.demo1.entity.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisApp {
public static void main(String[] args) throws IOException {
//加载配置,创建SqlSessionFactory
String resource = "mybatis.mapper/conf.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
//1.通过name查询一条记录
selectOneByName(sqlSession);
//2.添加一个用户
createUser(sqlSession);
//3.删除一个用户
deleteUser(sqlSession);
//4.修改一个用户
updateUser(sqlSession);
//5.查询所有用户
selectAllUser(sqlSession);
sqlSession.close();
}
private static void selectOneByName(SqlSession sqlSession){
User user = sqlSession.selectOne("com.mybatis.demo1.mapper.UserMapper.findUserByName", "xiaoming");
System.out.println(user.toString());
}
private static void createUser(SqlSession sqlSession){
User user = new User();
user.setName("peiqi");
user.setAge(16);
user.setSex("female");
sqlSession.insert("com.mybatis.demo1.mapper.UserMapper.createUser", user);
sqlSession.commit();
}
private static void deleteUser(SqlSession sqlSession){
sqlSession.delete("com.mybatis.demo1.mapper.UserMapper.deleteUser", "Joegy");
sqlSession.commit();
}
private static void updateUser(SqlSession sqlSession){
User user = sqlSession.selectOne("com.mybatis.demo1.mapper.UserMapper.findUserByName", "xiaoming");
user.setAge(28);
sqlSession.update("com.mybatis.demo1.mapper.UserMapper.updateUser", user);
sqlSession.commit();
}
private static void selectAllUser(SqlSession sqlSession){
List<User> userList = sqlSession.selectList("com.mybatis.demo1.mapper.UserMapper.selectAllUser");
System.out.println(userList);
}
}