配置mybatis文件
<?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>
<!-- 获取配置文件-->
<properties resource="db.properties"/>
<!-- 设置模型路径对象-->
<typeAliases>
<package name="cn.wolfcode.mybatis.domain"/>
</typeAliases>
<environments default="mybatis">
<environment id="mybatis">
<!-- 内置事物管理器-->
<transactionManager type="JDBC"/>
<!-- 连接池-->
<dataSource type="POOLED">
<!-- 配置文件内容 ${}-->
<property name="driver" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件的路径-->
<mapper resource="cn/wolfcode/mybatis/mapper/VideosMapper.xml"/>
</mappers>
</configuration>
配置映射文件(Videos)
<?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="cn.wolfcode.mybatis.mapper.VideosMapper">
<insert id="insert">
insert into videos(name,intro,author,releaseTime) values(#{name},#{intro},#{author},#{releaseTime})
</insert>
<delete id="delete">
delete from videos where id =#{id}
</delete>
<update id="update">
update videos set name = #{name}, intro = #{intro}, author = #{author},releaseTime = #{releaseTime}where id =#{id}
</update>
<select id="selectById" resultType="Videos">
select * from videos where id = #{id}
</select>
<select id="selectAll" resultType="Videos">
select * from videos
</select>
</mapper>
创建Videos接口(定义规范)
public interface IVideos {
void insert(Videos videos);
void delete(Long id);
void update(Videos videos);
Videos selectById(Long id);
List<Videos> selectAll();
}
创建Videos接口的实现类
public class VideosImpl implements IVideos {
//命名空间
String s = "cn.wolfcode.mybatis.mapper.VideosMapper.";
//增删改 都可以用 update方法
@Override
public void insert(Videos videos) {
//调用工具类(CRUD模板)中的update方法
MyBatisTemplate.update(statement("insert"), videos);
}
@Override
public void delete(Long id) {
MyBatisTemplate.update(statement("delete"), id);
}
@Override
public void update(Videos videos) {
MyBatisTemplate.update(statement("update"), videos);
}
/*
共同用的是返回集合的方法(selectList)
并运用了重载的方式调用方法
*/
@Override
public Videos selectById(Long id) {
//调用工具类(CRUD模板)中的select方法
List<Videos> videos = MyBatisTemplate.select(statement("selectById"), id);
return videos.get(0);
}
@Override
public List<Videos> selectAll() {
//调用工具类(CRUD模板)中的select方法
List<Videos> list = MyBatisTemplate.select(statement("selectAll"));
return list;
}
//命名空间=路径+SQL id 创建方法将其拼接
private String statement(String statementId) {
return s + statementId;
}
}
创建模型对象(Videos)
public class Videos {
private Long id;
private String name;//姓名
private String intro;//简介
private String author;//作家
private Date releaseTime;//时间
@Override
public String toString() {
return "Videos{" +
"id=" + id +
", name='" + name + '\'' +
", intro='" + intro + '\'' +
", author='" + author + '\'' +
", releaseTime=" + releaseTime.toLocaleString() +//调用本地时间格式
'}';
}
创建工具类(MyBatisTemplate)工具类模板 存放CRUD
public class MyBatisTemplate {
//增删改
public static int update(String s, Object o) {
// 通过调用工具类的静态方法 中的工厂对象 创建SqlSession
SqlSession sqlSession = MyBatisUtils.openSession();
int row = sqlSession.update(s, o);
sqlSession.commit();
sqlSession.close();
return row;
}
//查
//由于返回的对象可能是未知的 因此使用泛型
public static <T> T select(String s, Object o) {
SqlSession sqlSession = MyBatisUtils.openSession();
List<T> o1 = sqlSession.selectList(s, o);
sqlSession.commit();
sqlSession.close();
return (T) o1;
}
//方法重载
public static <T> T select(String s) {
return MyBatisTemplate.select(s, null);
}
}
创建第二个工具类(MyBatisUtils)存放在程序执行时 只需要运行一次的方法
public class MyBatisUtils {
private static SqlSessionFactory factory = null;
//在执行程序时 只需要 获取配置文件 和 创建工厂对象 一次 因此放在静态方法中
static {
InputStream in = null;
try {
//获取 mybatis 配置文件
in = Resources.getResourceAsStream("mybatis-config.xml");
//创建工厂对象
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//定义一个方法返回 SqlSession (类似于连接对象)
public static SqlSession openSession() {
return factory.openSession();
}
}
最后对程序进行增删改查的操作测试(测试先行)
public class VideosImplTest {
//自定义输入时间
public static Date getTime(String str) {
// 将所需要的格式传入
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
// 将传入的字符串解析成Date类型
parse = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
//创建实现类对象
IVideos iVideos = new VideosImpl();
//插入
@Test
public void insert() {
Videos videos = new Videos(null, "011D123WDw", "ddwadwa", "wadwad",
VideosImplTest.getTime("2022-01-01 11:12:32"));
iVideos.insert(videos);
}
//删除
@Test
public void delete() {
iVideos.delete(1l);
}
//修改/更新
@Test
public void update() {
Videos videos = new Videos(2l, "小明", "简介", "作者", new Date());//调用系统当前时间
iVideos.update(videos);
}
//查询指定单条语句
@Test
public void selectById() {
Videos videos = iVideos.selectById(2l);
System.out.println("videos = " + videos);
}
//查询所有语句
@Test
public void selectAll() {
List<Videos> videos = iVideos.selectAll();
videos.forEach(System.out::println);
}
}
目录:
测试结果