9.Spring的IOC+MyBaits+log4j+Servlet

Posted 红酒人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了9.Spring的IOC+MyBaits+log4j+Servlet相关的知识,希望对你有一定的参考价值。

1.创建如下mysql脚本

 1 drop table if exists book_info;
 2 
 3 create table if not exists book_info(
 4 book_id int(10) primary key auto_increment,
 5 book_name varchar(50) not null,
 6 book_author varchar(50) not null,
 7 book_publish varchar(50) not null,
 8 book_page int(10) not null,
 9 book_price double(10,2) not null,
10 creation_time date not null
11 );
12 
13 insert into book_info
14 (book_name,book_author,book_publish,book_page,book_price,creation_time) 
15 values
16 (\'Java与模式\',\'闫磊\',\'电子工业出版社\',1100,97.00,\'2017-01-01\'),
17 (\'Java核心\',\'(美)Cay S.Horstmann\',\'机械工业出版社\',678,333.00,\'2017-01-02\'),
18 (\'Java编程思想\',\'(美)Bruce Exkel\',\'机械工业出版社\',590,75.00,\'2017-01-03\'),
19 (\'TCP/IP详解\',\'(美)W.Ricahr Steves\',\'机械工业出版社\',205,90.20,\'2017-01-04\'),
20 (\'什么是数学\',\'R.阿朗\',\'复旦大学出版社\',521,42.00,\'2017-01-05\')
21 ;
22 
23 select * from book_info;
book_info

2.创建项目Spring_Mybatis_Exam9

 

3.导入jar文件,注意:mysql是jar文件版本:mysql-connector-java-5.1.7-bin.jar或更高版本

 

4.在src下的com.pojo包下创建BookInfo.java

 1 package com.pojo;
 2 import java.io.Serializable;
 3 public class BookInfo implements Serializable{
 4     private static final long serialVersionUID = 1L;
 5     private Integer book_id      ;
 6     private Integer book_page    ;
 7     private Double  book_price   ;
 8     private String  book_name    ;
 9     private String  book_author  ;
10     private String  book_publish ;
11     private String  creation_time;
12     
13     public BookInfo() {
14     }
15     
16     public BookInfo(Integer bookPage, Double bookPrice, String bookName,
17             String bookAuthor, String bookPublish, String creationTime) {
18         book_page = bookPage;
19         book_price = bookPrice;
20         book_name = bookName;
21         book_author = bookAuthor;
22         book_publish = bookPublish;
23         creation_time = creationTime;
24     }
25 
26     public BookInfo(Integer bookId, Integer bookPage, Double bookPrice,
27             String bookName, String bookAuthor, String bookPublish,
28             String creationTime) {
29         book_id = bookId;
30         book_page = bookPage;
31         book_price = bookPrice;
32         book_name = bookName;
33         book_author = bookAuthor;
34         book_publish = bookPublish;
35         creation_time = creationTime;
36     }
37     public Integer getBook_id() {
38         return book_id;
39     }
40     public void setBook_id(Integer bookId) {
41         book_id = bookId;
42     }
43     public Integer getBook_page() {
44         return book_page;
45     }
46     public void setBook_page(Integer bookPage) {
47         book_page = bookPage;
48     }
49     public Double getBook_price() {
50         return book_price;
51     }
52     public void setBook_price(Double bookPrice) {
53         book_price = bookPrice;
54     }
55     public String getBook_name() {
56         return book_name;
57     }
58     public void setBook_name(String bookName) {
59         book_name = bookName;
60     }
61     public String getBook_author() {
62         return book_author;
63     }
64     public void setBook_author(String bookAuthor) {
65         book_author = bookAuthor;
66     }
67     public String getBook_publish() {
68         return book_publish;
69     }
70     public void setBook_publish(String bookPublish) {
71         book_publish = bookPublish;
72     }
73     public String getCreation_time() {
74         return creation_time;
75     }
76     public void setCreation_time(String creationTime) {
77         creation_time = creationTime;
78     }
79     @Override
80     public String toString() {
81         return "BookInfo [book_author=" + book_author + ", book_id=" + book_id
82                 + ", book_name=" + book_name + ", book_page=" + book_page
83                 + ", book_price=" + book_price + ", book_publish="
84                 + book_publish + ", creation_time=" + creation_time + "]";
85     } 
86 }
BookInfo.java

5.在src下的com.page包下创建PageUtil.java

 1 package com.page;
 2 
 3 public class PageUtil {
 4     private Integer pageno;
 5     private Integer pagesize;
 6     private Integer totalpage;
 7     private Integer totalcount;
 8     private Integer startrow;
 9     
10     public PageUtil() {
11     }
12 
13     public PageUtil(Integer pageno, Integer pagesize, Integer totalcount) {
14         this.pageno = pageno;
15         this.pagesize = pagesize;
16         this.totalcount = totalcount;
17         this.setTotalpage(totalcount,pagesize);
18         this.setStartrow(pageno,pagesize);
19     }
20 
21     public void setStartrow(Integer pageno, Integer pagesize) {
22         this.startrow=(pageno-1)*pagesize;
23     }
24 
25     public void setTotalpage(Integer totalcount, Integer pagesize) {
26         this.totalpage=totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1;
27         
28     }
29 
30     public Integer getPageno() {
31         return pageno;
32     }
33 
34 
35     public void setPageno(Integer pageno) {
36         this.pageno = pageno;
37     }
38 
39 
40     public Integer getPagesize() {
41         return pagesize;
42     }
43 
44 
45     public void setPagesize(Integer pagesize) {
46         this.pagesize = pagesize;
47     }
48 
49 
50     public Integer getTotalpage() {
51         return totalpage;
52     }
53 
54 
55     
56 
57 
58     public Integer getTotalcount() {
59         return totalcount;
60     }
61 
62 
63     public void setTotalcount(Integer totalcount) {
64         this.totalcount = totalcount;
65     }
66 
67 
68     public Integer getStartrow() {
69         return startrow;
70     }
71 
72 }
PageUtil.java

6.在src下的com.mapper包下创建BookInfoMapper.java

 1 package com.mapper;
 2 import java.util.List;
 3 import com.page.PageUtil;
 4 import com.pojo.BookInfo;
 5 public interface BookInfoMapper {
 6     //分页查询
 7     List<BookInfo> findPage(PageUtil pageutil);
 8     //查询总条数
 9     Integer getTotalCout();
10     //根据主键查询对象
11     BookInfo findByPrimaryKey(Integer key);
12     //添加对象
13     Integer insertBookInfo(BookInfo bookInfo);
14     //根据主键修改对象
15     Integer updatePrimaryKey(BookInfo bookInfo);
16     //根据主键删除
17     Integer deletePrimaryKey(Integer key);
18 }
BookInfoMapper.java

7.在src下的com.mapper包下创建BookInfoMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.mapper.BookInfoMapper">
 4     <!-- 分页查询 -->
 5     <select id="findPage" parameterType="PageUtil" resultType="BookInfo">
 6        select * from book_info limit #{startrow},#{pagesize}
 7     </select> 
 8     <!-- 获取总条数 -->
 9     <select id="getTotalCout" resultType="Integer">
10        select count(*) from book_info
11     </select>
12     <!-- 查询单条 -->
13     <select id="findByPrimaryKey" parameterType="Integer" resultType="BookInfo">
14        select * from book_info where book_id=#{book_id}
15     </select>
16     <!-- 添加 -->
17     <insert id="insertBookInfo" parameterType="BookInfo">
18        insert into 
19        book_info(book_name,book_author,book_publish,book_page,book_price,creation_time) 
20        values(#{book_name},#{book_author},#{book_publish},#{book_page},#{book_price},#{creation_time})
21     </insert>
22     <!-- 修改 -->
23     <update id="updatePrimaryKey" parameterType="BookInfo">
24        update book_info 
25        set
26            book_name=#{book_name},
27            book_author=#{book_author},
28            book_publish=#{book_publish},
29            book_page=#{book_page},
30            book_price=#{book_price},
31            creation_time=#{creation_time} 
32        where 
33            book_id=#{id}
34     </update>
35     <!-- 删除 -->
36     <delete id="deletePrimaryKey" parameterType="Integer">
37        delete from book_info where book_id=#{book_id}
38     </delete>
39 </mapper>
BookInfoMapper.xml

8.在src下创建log4j.properties

 1 log4j.rootLogger=DEBUG, Console  
 2   
 3 #Console  
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
 7   
 8 log4j.logger.org.apache=INFO  
 9 log4j.logger.java.sql.ResultSet=INFO  
10 log4j.logger.java.sql.Connection=DEBUG  
11 log4j.logger.java.sql.Statement=DEBUG  
12 log4j.logger.java.sql.PreparedStatement=DEBUG   
log4j.properties

9.在src下创建mybaits-config.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
 3 <configuration>
 4    <!-- 别名配置 -->
 5    <typeAliases>
 6       <typeAlias type="com.page.PageUtil" alias="PageUtil"/>
 7       <typeAlias type="com.pojo.BookInfo" alias="BookInfo"/>
 8    </typeAliases>
 9    
10    <!-- 和spring结合时,数据源配置在spring -->
11    
12    <!-- 注册映射xml -->
13    <mappers>
14      <mapper resource="com/mapper/BookInfoMapper.xml"/>
15    </mappers>
16 </configuration>
mybaits-config.xml

10.在src下的com.mapper.impl包下创建BookInfoMapperImpl.java

 1 package com.mapper.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.session.SqlSessionManager;
 6 import org.mybatis.spring.SqlSessionTemplate;
 7 
 8 import com.mapper.BookInfoMapper;
 9 import com.page.PageUtil;
10 import com.pojo.BookInfo;
11 
12 public class BookInfoMapperImpl implements BookInfoMapper {
13     //定义sqlsession模板对象的
14     private SqlSessionTemplate sqlSessionTemplate;
15     //提供sqlsqlsession模板对象的getter和setter
16     public SqlSessionTemplate getSqlSessionTemplate() {
17         return sqlSessionTemplate;
18     }
19     public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
20         this.sqlSessionTemplate = sqlSessionTemplate;
21     }
22     
23     //------------------------------
24     public Integer deletePrimaryKey(Integer key) {
25         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
26         return mapper.deletePrimaryKey(key);
27     }
28 
29     public BookInfo findByPrimaryKey(Integer key) {
30         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
31         return mapper.findByPrimaryKey(key);
32     }
33 
34     public List<BookInfo> findPage(PageUtil pageutil) {
35         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
36         return mapper.findPage(pageutil);
37     }
38 
39     public Integer getTotalCout() {
40         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
41         return mapper.getTotalCout();
42     }
43 
44     public Integer insertBookInfo(BookInfo bookInfo) {
45         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
46         return mapper.insertBookInfo(bookInfo);
47     }
48 
49     public Integer updatePrimaryKey(BookInfo bookInfo) {
50         BookInfoMapper mapper=sqlSessionTemplate.getMapper(BookInfoMapper.class);
51         return mapper.updatePrimaryKey(bookInfo);
52     }
53 
54 }
BookInfoMapperImpl.java

11.在src下的com.service包下创建BookInfoService.java

 1 package com.service;
 2 import java.util.List;
 3 import com.page.PageUtil;
 4 import com.pojo.BookInfo;
 5 public interface BookInfoService {
 6     //分页查询
 7     List<BookInfo> queryAll(PageUtil pageutil);
 8     //查询总条数
 9     Integer getTotalCout();
10     //根据主键查询对象
11     BookInfo findBookInfo(Integer key);
12     //添加对象
13     Integer saveBookInfo(BookInfo bookInfo);
14     //根据主键修改对象
15     Integer editBookInfo(BookInfo bookInfo);
16     //根据主键删除
17     Integer deleteBookInfo(Integer key);
18 }
BookInfoService.java

12.在src下的com.service.impl包下创建BookInfoServiceImpl.java

 1 以上是关于9.Spring的IOC+MyBaits+log4j+Servlet的主要内容,如果未能解决你的问题,请参考以下文章

java-mybaits-00103-入门程序原生的查增删改

Mybaits简诉

Spring-AOP切面编程

Spring_9-Spring5新特性

spring和mybaits整合

Mybaits 快速入门代码实例(maven代码版)