1、题目描述
2、参考解答
com.controller包下的BookController.java
package com.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.dao.BookDao;
import com.po.MyBook;
@Controller("bookController")
public class BookController {
@Autowired
private BookDao bookDao;
public void test() {
//查询一条书籍记录
MyBook abook = bookDao.selectBookBySN(1);
System.out.println(abook);
System.out.println("================");
//添加一条书籍记录
MyBook addmb = new MyBook();
addmb.setBookName("西游记");
addmb.setBookPrice((float) 24.6);
int add = bookDao.addBook(addmb);
System.out.println("添加了" + add + "条记录");
System.out.println("================");
//修改一条书籍记录
MyBook updatemb = new MyBook();
updatemb.setSn(1);
updatemb.setBookName("红楼梦");
updatemb.setBookPrice((float) 35.3);
int up = bookDao.updateBook(updatemb);
System.out.println("修改了" + up + "条记录");
System.out.println( "================");
//删除一条书籍记录
int dl = bookDao.deleteBook(9);
System.out.println("删除了" + dl + "条记录");
System.out.println("================");
//查询所有书籍记录
List list = bookDao.selectAllBook();
for (MyBook myBook : list) {
System.out.println(myBook);
}
}
}
com.controller包下的TestController.java
package com.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestController {
public static void main(String[] args) {
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
BookController bc = (BookController)appCon.getBean("bookController");
bc.test();
}
}
com.dao包下的BookDao.java
package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.po.MyBook;
@Repository("bookDao")
@Mapper
/*使用Spring自动扫描MyBatis的接口并装配
(Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口*/
public interface BookDao {
/**
* 接口方法对应SQL映射文件UserMapper.xml中的id
*/
public MyBook selectBookBySN(Integer sn);
public List selectAllBook();
public int addBook(MyBook book);
public int updateBook(MyBook book);
public int deleteBook(Integer sn);
}
com.mybatis包下的BookMapper.xml
insert into book (bookName,bookPrice) values(#{bookName},#{bookPrice})
update book set bookName = #{bookName},bookPrice = #{bookPrice} where sn=#{sn}
delete from book where sn=#{sn}
com.mybatis包下的mybatis-config.xml
com.mybatis.mapper包下的BookMapper.xml
insert into book (bookName,bookPrice) values(#{bookName},#{bookPrice})
update book set bookName = #{bookName},bookPrice = #{bookPrice} where sn = #{sn}
delete from user where sn = #{sn}
com.mybatis.po包下的MyBook.java
package com.mybatis.po;
/**
*springtest数据库中book表的持久化类
*/
public class MyBook {
private Integer sn;//主键
private String bookName;
private Float bookPrice;
public Integer getSn() {
return sn;
}
public void setSn(Integer sn) {
this.sn = sn;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getBookPrice() {
return bookPrice;
}
public void setBookPrice(Float bookPrice) {
this.bookPrice = bookPrice;
}
@Override
public String toString() {
return "MyBook [sn=" + sn + ", bookName=" + bookName + ", bookPrice=" + bookPrice + "]";
}
}
com.mybatis.test包下的MyBatisTest.java
package com.mybatis.test;
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.mybatis.po.MyBook;
public class MyBatisTest {
public static void main(String[] args) {
try {
//读取配置文件mybatis-config.xml
InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
//根据配置文件构建SqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
//通过SqlSessionFactory创建SqlSession
SqlSession ss = ssf.openSession();
//SqlSession执行映射文件中定义的SQL,并返回映射结果
//com.mybatis.mapper.UserMapper.selectUserById为UserMapper.xml中的命名空间+select的id
//查询一条书籍记录
MyBook mb = ss.selectOne("com.mybatis.mapper.BookMapper.selectBookBySN", 1);
System.out.println(mb);
//添加一条书籍记录
MyBook addmb = new MyBook();
addmb.setSn(24);
addmb.setBookName("三国演义");
addmb.setBookPrice((float) 50.2);
ss.insert("com.mybatis.mapper.BookMapper.addBook",addmb);
//修改一条书籍记录
MyBook updatemb = new MyBook();
updatemb.setSn(1);
updatemb.setBookName("水浒传");
updatemb.setBookPrice((float) 40.1);
ss.update("com.mybatis.mapper.BookMapper.updateBook", updatemb);
//删除一条书籍记录
ss.delete("com.mybatis.mapper.BookMapper.deleteBook", 3);
//查询所有书籍记录
List listMb = ss.selectList("com.mybatis.mapper.BookMapper.selectAllBook");
for (MyBook myBook : listMb) {
System.out.println(myBook);
}
//提交事务
ss.commit();
//关闭SqlSession
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
com.po包下的MyBook.java
package com.po;
/**
*springtest数据库中book表的持久化类
*/
public class MyBook {
private Integer sn;//主键
private String bookName;
private Float bookPrice;
public Integer getSn() {
return sn;
}
public void setSn(Integer sn) {
this.sn = sn;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getBookPrice() {
return bookPrice;
}
public void setBookPrice(Float bookPrice) {
this.bookPrice = bookPrice;
}
@Override
public String toString() {
return "MyBook [sn=" + sn + ", bookName=" + bookName + ", bookPrice=" + bookPrice + "]";
}
}
applicationContext.xml
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n