mybatis-config.xml
<?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>
<!-- 配置数据库连接信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="hr"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置mapper文件的路径 -->
<mappers>
<mapper resource="com/gxy/dao/TeacherMapper.xml"/>
<mapper resource="com/gxy/dao/BookMapper.xml"/>
</mappers>
</configuration>
BookMaper.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.gxy.dao.BookDao"> <!-- 找到dao层类对象 -->
<!-- 启用缓存功能 -->
<cache></cache>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.gxy.entity.Book">
<id column="book_id" property="bookId" />
<result column="book_name" property="bookName" />
<result column="book_auth" property="bookAuth" />
<result column="book_brief" property="bookBrief" />
<result column="book_create_date" property="bookCreateDate" />
</resultMap>
<!-- id全局唯一 -->
<!-- 查询list -->
<select id="selectBookList" resultMap="BaseResultMap">
select * from book
</select>
<!-- 插入 -->
<insert id="insertBook">
insert into book (book_id,book_name,book_auth,book_brief)
values(seq_bank_account.nextval,#{bookName,jdbcType=VARCHAR},#{bookAuth,jdbcType=VARCHAR},#{bookBrief,jdbcType=VARCHAR})
</insert>
<!-- 修改 -->
<update id="updateBook">
update book set book_name=#{bookName} where book_id=#{bookId}
</update>
<!-- 删除 -->
<delete id="delectBook">
delete from book where book_name=#{bookName}</delete>
<!-- 模糊查询 -->
<select id="selectlike" resultMap="BaseResultMap">
select * from book where book_name like '%${bookName}%'
</select>
<!-- 多条件查询中间包括以后必须添加and -->
<select id="selectCondition" resultMap="BaseResultMap">
select * from book
<where>
<if test="bookName !=null and bookName !=''">
book_name= #{bookName}
</if>
<if test="bookAuth !=null and bookAuth !=''">
and book_auth = #{bookAuth}
</if>
<if test="bookBrief !=null and bookBrief !=''">
and book_brief = #{bookBrief}
</if>
</where>
</select>
</mapper>
Book.java
package com.gxy.entity;
import java.io.Serializable;
import java.sql.Date;
public class Book implements Serializable{
/**
* 给每一个实体类一个唯一的名字,可加可不加
*/
private static final long serialVersionUID = 1L;
private int bookId;//图书id
private String bookName;//图书名称
private String bookAuth;
private String bookBrief;
private Date bookCreateDate;//时间
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuth() {
return bookAuth;
}
public void setBookAuth(String bookAuth) {
this.bookAuth = bookAuth;
}
public String getBookBrief() {
return bookBrief;
}
public void setBookBrief(String bookBrief) {
this.bookBrief = bookBrief;
}
public Date getBookCreateDate() {
return bookCreateDate;
}
public void setBookCreateDate(Date bookCreateDate) {
this.bookCreateDate = bookCreateDate;
}
public Book(int bookId, String bookName, String bookAuth, String bookBrief, Date bookCreateDate) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookAuth = bookAuth;
this.bookBrief = bookBrief;
this.bookCreateDate = bookCreateDate;
}
public Book() {
super();
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookAuth=" + bookAuth + ", bookBrief=" + bookBrief
+ ", bookCreateDate=" + bookCreateDate + "]";
}
}
BookDao.javaj
接口
package com.gxy.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.gxy.entity.Book;
public interface BookDao {
//查询list 方法名需要与id名称一致
public List<Book> selectBookList();
//插入
public int insertBook(@Param("bookName")String bookName,
@Param("bookAuth")String bookAuth,
@Param("bookBrief")String bookBrief);
//修改
public int updateBook(@Param("bookName")String bookName,
@Param("bookId")int bookId);
//删除
public int delectBook(@Param("bookName")String bookName);
//模糊查询
public List<Book> selectlike(@Param("bookName")String bookName);
//多条件查询
public List<Book> selectCondition(Book book);
}
Bookservice.java
接口
package com.gxy.service;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.gxy.entity.Book;
public interface BookService {
//查询list 与dao层方法相同
public List<Book> selectBookList() throws IOException;
//插入
public int insertBook(@Param("bookName")String bookName,
@Param("bookAuth")String bookAuth,
@Param("bookBrief")String bookBrief)throws IOException;
//修改
public int updateBook(@Param("bookName")String bookName,
@Param("bookId")int bookId)throws IOException;
//删除
public int delectBook(@Param("bookName")String bookName) throws IOException;
//模糊查询
public List<Book> selectlike(@Param("bookName")String bookName) throws IOException;
//多条件查询
public List<Book> selectCondition(Book book) throws IOException;
}
BookserviceImpl.java
package com.gxy.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 com.gxy.dao.BookDao;
import com.gxy.entity.Book;
import com.gxy.service.BookService;
import com.gxy.util.MyBatisUtil;
public class BookServiceImpl implements BookService{
@Override
public List<Book> selectBookList() throws IOException {
// TODO Auto-generated method stub
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
//dao层正确的方法
List<Book> list = bookDao.selectBookList();
//关闭流
session.close();
MyBatisUtil.close();
return list;
}
@Override
public int insertBook(String bookName, String bookAuth, String bookBrief) throws IOException {
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
int i = bookDao.insertBook(bookName, bookAuth, bookBrief);
//事务并不是主动提交而是需要手动提交
session.commit();
MyBatisUtil.close();
return i;
}
@Override
public int delectBook(String bookName) throws IOException {
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
int i = bookDao.delectBook(bookName);
//事务并不是主动提交而是需要手动提交
session.commit();
MyBatisUtil.close();
return i;
}
@Override
public int updateBook(String bookName, int bookId) throws IOException {
// TODO Auto-generated method stub
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
int i = bookDao.updateBook(bookName, bookId);
//事务并不是主动提交而是需要手动提交
session.commit();
MyBatisUtil.close();
return i;
}
@Override
public List<Book> selectlike(String bookName) throws IOException {
// TODO Auto-generated method stub
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
//dao层正确的方法
List<Book> list = bookDao.selectlike(bookName);
MyBatisUtil.close();
return list;
}
@Override
public List<Book> selectCondition(Book book) throws IOException {
// TODO Auto-generated method stub
SqlSession session = MyBatisUtil.getSqlSession();
BookDao bookDao = session.getMapper(BookDao.class);
//dao层正确的方法
List<Book> list = bookDao.selectCondition(book);
return list;
}
}
MyBatisUtil.java
package com.gxy.util;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private static SqlSessionFactory factory;
//保证service层 进行多次dao操作,公用一个SqlSession对象
private static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>();
static{
try {
String resouce = "mybatis-config.xml"; //代表总配置文件的路径
InputStream is = Resources.getResourceAsStream(resouce);//使用输入流读取配置文件
factory = new SqlSessionFactoryBuilder().build(is);//facotry只需要创建一个
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession session = tl.get();
if(session == null) {
session = factory.openSession();
tl.set(session);
}
return session;
}
public static void close(){
SqlSession session = tl.get();
if(session != null){
session.close();
tl.set(null);
}
}
}
TestMain.java
package com.gxy.Test;
import java.io.IOException;
import java.util.List;
import com.gxy.entity.Book;
import com.gxy.service.BookService;
import com.gxy.service.impl.BookServiceImpl;
public class TestMain2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Book book1 = new Book();
BookService book = new BookServiceImpl();
//List<Book> list = book.selectBookList();//查询操作
//int i = book.insertBook("浮生物语", "婆娑双树", "奇门异事");//插入操作
// int i = book.delectBook("斗破苍穹");
//int i = book.updateBook("wude", 22);
//List<Book> list = book.selectlike("斗");//模糊查询
book1.setBookName("斗罗大陆");
book1.setBookAuth("唐三");
List<Book> list = book.selectCondition(book1);
System.out.println(list);
}
}