对数据库mydb中的图书表(t_book),在映射文件中配置以下操作的sql语句,并在测试类中为每条语句书写单独的测试模块进行验证。
(1)根据id查询图书信息
(2)根据图书名称模糊查询相关图书信息
(3)添加图书
(4)根据id修改图书
(5)根据id删除图书
(6)有如下查询界面配置单击查询按钮,完成用户查询要求时的查询语句。
(7)有如下批量删除界面用户可以同时勾选要删除的多本图书,完成批量删除操作,请配置相应操作语句。
**
**
**
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>zjwTest01artifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.1version>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
project>
**
**
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.zjw.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com/zjw/mapper"/>
mappers>
configuration>
**
**
package com.zjw.pojo;
public class Book {
private Integer id;
private String name;
private String author;
private String press;
private Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", press='" + press + '\'' +
", price=" + price +
'}';
}
}
接口方法中的参数
只有一个数组参数或集合参数
默认情况:集合collection=list, 数组是collection=array
推荐:使用 @Param 来指定参数的名称
多参数
多参数请使用 @Param 来指定, 否则SQL中会很不方便
参数是Map 指定为 Map 中的对应的 Key 即可。其实上面的 @Param 最后也是转化为 Map 的。
参数是对象 使用属性.属性即可。
package com.zjw.mapper;
import com.zjw.pojo.Book;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//(1)根据id查询图书信息
Book findbyid(@Param("id") Integer id);
//(2)根据图书名称模糊查询相关图书信息
List<Book> findbyname(@Param("name") String name);
//(3)添加图书
void insertbook(Book book);
//(4)根据id修改图书
int updatebook(Book book);
//(5)根据id删除图书
void deleteById(@Param("id") Integer id);
//(6)完成用户查询要求时的查询语句(图书名称,作者,出版社)
List<Book> findByCondition(@Param("name") String name, @Param("author") String author, @Param("press") String press);
// List findByCondition(Book book);
// List findByCondition(Map map);
//(7)根据id批量删除图书
void deleteByIds(@Param("ids") int[] ids);
}
**
**
在资源目录同级文件夹下(见IDEA目录文件的截图)
<mapper namespace="com.zjw.mapper.BookMapper">
<select id="findbyid" resultType="com.zjw.pojo.Book">
select *
from t_book
where id = #{id}
select>
<select id="findbyname" resultType="com.zjw.pojo.Book">
select *
from t_book
where name like concat('%', #{name}, '%')
select>
<insert id="insertbook" useGeneratedKeys="true" keyProperty="id">
insert into t_book (name, author, press, price)
values (#{name}, #{author}, #{press}, #{price})
insert>
<update id="updatebook">
update t_book
<set>
<if test="name !=null and name !='' ">name=#{name},
if>
<if test="author !=null and author !=' '">author=#{author},
if>
<if test="press !=null and press !='' ">press=#{press},
if>
<if test="price !=null ">price=#{price}
if>
set>
where id = #{id};
update>
<delete id="deleteById">
delete
from t_book
where id = #{id}
delete>
<select id="findByCondition" resultType="com.zjw.pojo.Book">
select *
from t_book
<where>
<if test="name != null and name !=''">and name like concat('%', #{name}, '%')if>
<if test="author != null and author !=''">and author like concat('%', #{author}, '%')if>
<if test="press != null and press !='' ">and press like concat('%', #{press}, '%')if>
where>
select>
<delete id="deleteByIds">
delete
from t_book
where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
foreach>;
delete>
mapper>
package com.zjw.utils;
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.Reader;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSessioin() {
return sqlSessionFactory.openSession();
}
}
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
mysql.username=root
mysql.password=1669
import com.zjw.mapper.BookMapper;
import com.zjw.pojo.Book;
import com.zjw.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class BookTest {
//根据id查询图书信息
@Test
public void FindByID() {
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//对象调用方法
Book book = bookMapper.findbyid(1);
System.out.println(book.toString());
//提交
session.commit();
//释放资源
session.close();
}
//根据图书名称模糊查询相关图书信息
@Test
public void FindByName() {
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//对象调用方法
List<Book> book = bookMapper.findbyname("Java");
System.out.println(book);
//提交
session.commit();
//释放资源
session.close();
}
//添加图书
@Test
public void InsertBook() {
String name = "海底两万里";
String author = "凡尔纳";
String press = "清华出版社";
Integer price = 88;
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//对象调用方法
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPress(press);
book.setPrice(price);
//执行文件
bookMapper.insertbook(book);
//提交事务
session.commit();
//释放资源
session.close();
}
//根据id修改图书
@Test
public void UpdateBook() {
Integer id = 1;
String press = "清华出版社";
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//对象调用方法
Book book = new Book();
book.setPress(press);
book.setId(id);
//执行文件
bookMapper.updatebook(book);
//提交事务
session.commit();
//释放资源
session.close();
}
//根据id删除图书
@Test
public void DeleteById() {
Integer id = 1;
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//执行文件
bookMapper.deleteById(id);
//提交事务
session.commit();
//释放资源
session.close();
}
//完成用户查询要求时的查询语句(图书名称,作者,出版社)
//这里演示的是无交互模式
@Test
public void findBycondition() {
String name = "海底两万里";
String author = "凡尔纳";
String press = "清华出版社";
/*Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPress(press);*/
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
List<Book> books = bookMapper.findByCondition(name, author, press);
System.out.println(books);
//提交
session.commit();
//释放资源
session.close();
}
//批量删除
@Test
public void DeleteByIds() {
int[] ids = {9, 10, 11};
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//执行文件
bookMapper.deleteByIds(ids);
//提交事务
session.commit();
//释放资源
session.close();
}
}
**
**
因为@Test控制台为只读模式,故新建addBook类来实现交互,完成自定义添加
import com.zjw.mapper.BookMapper;
import com.zjw.pojo.Book;
import com.zjw.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.Scanner;
public class addBook {
public static void main(String[] args) {
System.out.println("请输入书名");
Scanner sc1 = new Scanner(System.in);
String name = sc1.nextLine();
System.out.println("请输入作者");
Scanner sc2 = new Scanner(System.in);
String author = sc2.nextLine();
System.out.println("请输入出版社");
Scanner sc3 = new Scanner(System.in);
String press = sc3.nextLine();
System.out.println("请输入价格");
Scanner sc4 = new Scanner(System.in);
Integer price = sc4.nextInt();
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
//对象调用方法
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPress(press);
book.setPrice(price);
//执行文件
bookMapper.insertbook(book);
//提交事务
session.commit();
//释放资源
session.close();
}
}
**
**
同理,新建findBycondition类来实现交互,完成查询
import com.zjw.mapper.BookMapper;
import com.zjw.pojo.Book;
import com.zjw.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.Scanner;
public class findBycondition {
public static void main(String[] args) {
System.out.println("请输入书名");
Scanner sc1 = new Scanner(System.in);
String name = sc1.nextLine();
System.out.println("请输入作者");
Scanner sc2 = new Scanner(System.in);
String author = sc2.nextLine();
System.out.println("请输入出版社");
Scanner sc3 = new Scanner(System.in);
String press = sc3.nextLine();
// String name="海底两万里";
// String author="凡尔纳";
// String press="清华出版社";
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPress(press);
SqlSession session = MybatisUtils.getSessioin();
BookMapper bookMapper = session.getMapper(BookMapper.class);
List<Book> books = bookMapper.findByCondition(name, author, press);
// List books = bookMapper.findByCondition(book);
System.out.println(books);
//提交
session.commit();
//释放资源
session.close();
}
}
如果复制后无法运行,参照java 错误 无效的源发行版:XX 解决方法