mybatis的增删改查

一、配置文件方式

mapper.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.hy.mybatis.test.News"> <insert id="insertNews" parameterType="com.hy.mybatis.test.News">
insert into news(title,content,author,createdate) values(#{title},#{content},#{author},#{createdate})
</insert> <delete id="deleteNews" parameterType="int">
delete from news where id= #{id}
</delete> <update id="updateNews" parameterType="com.hy.mybatis.test.News">
update news set author = #{author} where id = #{id}
</update> <select id="getAllNews" resultType="com.hy.mybatis.test.News">
select * from news
</select> <select id="getNews" parameterType="int" resultType="com.hy.mybatis.test.News">
select * from news where id=#{id}
</select>
</mapper>

引入:

<mappers>
<mapper resource="com/hy/mybatis/test/News.xml"/>
</mappers>

测试:

public static SqlSession getSqlSession() {
InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
return factory.openSession(true);
}

封装获取sqlSession的方法,这里采用自动提交事务的方式。

@Test
public void testAddNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.insertNews";
int insert = session.insert(statement, new News(-1, "android学习", "学习",
"李博", new Date()));
System.out.println(insert);
} @Test
public void testDeleteNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.deleteNews";
int delete = session.delete(statement, 2);
System.out.println(delete);
} @Test
public void testUpdateNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.updateNews";
int update = session.update(statement, new News(3, "", "", "xx",
new Date()));
System.out.println(update);
} @Test
public void testgetAllNews() {
SqlSession session = MyBatisUtil.getSqlSession();
String statement = "com.hy.mybatis.test.News.getAllNews";
List<News> list = session.selectList(statement);
System.out.println(list);
}

二、注解方式

写一个mapperInterface:

package com.hy.mybatis.test;

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; public interface NewsInterface { @Insert("insert into news(title,content,author,createdate) values(#{title},#{content},#{author},#{createdate})")
public int add(News news); @Delete("delete from news where id= #{id}")
public int deleteById(int id); @Update("update news set author = #{author} where id = #{id}")
public int update(News news); @Select("select * from news")
public List<News> getAllNews(); }

测试代码:

@Test
public void testAddNews2() {
SqlSession session = MyBatisUtil.getSqlSession();
NewsInterface newsInterface= session.getMapper(NewsInterface.class);
int add = newsInterface.add(new News(-1, "android学习", "学习",
"李博", new Date()));
System.out.println(add);
}

先获取一个mapperInterface的示例,然后调用就行了!!

上一篇:QJsonDocument实现Qt下JSON文档读写


下一篇:HTTPS Web配置举例