1.pom.xml配置
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.web.xml配置
1 <servlet> 2 <servlet-name>springmvc</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:applicationConfig.xml</param-value> 7 </init-param> 8 <load-on-startup>1</load-on-startup> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>springmvc</servlet-name> 12 <url-pattern>/</url-pattern> 13 </servlet-mapping> 14 <!--字符乱码--> 15 <filter> 16 <filter-name>EncodingFilter</filter-name> 17 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 18 <init-param> 19 <param-name>encoding</param-name> 20 <param-value>utf-8</param-value> 21 </init-param> 22 </filter> 23 <filter-mapping> 24 <filter-name>EncodingFilter</filter-name> 25 <url-pattern>/*</url-pattern> 26 </filter-mapping>
3.数据库连接配置文件
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/books?useUnicode=true&characterEncoding=utf8
3 username= 4 password=
4.mybatis配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 4 5 <configuration> 6 <typeAliases> 7 <typeAlias type="com.wu.pojo"/> 8 </typeAliases> 9 </configuration>
5.dao层配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:property-placeholder location="classpath:db.properties"/> 11 12 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 13 <property name="driverClass" value="${driver}"/> 14 <property name="jdbcUrl" value="${url}"/> 15 <property name="user" value="${username}"/> 16 <property name="password" value="${password}"/> 17 <property name="maxPoolSize" value="30"/> 18 <property name="minPoolSize" value="10"/> 19 <property name="autoCommitOnClose" value="false"/> 20 <property name="checkoutTimeout" value="100000"/> 21 <property name="acquireRetryAttempts" value="2"/> 22 </bean> 23 24 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 25 <property name="dataSource" ref="dataSource"/> 26 <property name="configLocation" value="classpath:mybatis-config.xml"/> 27 </bean> 28 29 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 30 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 31 <property name="basePackage" value="com.wu.dao"/> 32 </bean> 33 34 </beans>
6.service层配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="com.wu.service"/> 11 12 <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> 13 <property name="dataSource" ref="dataSource"/> 14 </bean> 15 16 </beans>
7.mvc配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <mvc:annotation-driven/> 14 <mvc:default-servlet-handler/> 15 <context:component-scan base-package="com.wu.controller"/> 16 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/jsp/"/> 19 <property name="suffix" value=".jsp"/> 20 </bean> 21 22 </beans>
8.整体配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <import resource="classpath:spring-dao.xml"/> 11 <import resource="classpath:spring-service.xml"/> 12 <import resource="classpath:spring-mvc.xml"/> 13 14 15 </beans>
9.实体类
1 package com.wu.pojo; 2 3 public class Books { 4 private int bookId; 5 private String bookName; 6 private int bookCounts; 7 private String bookDetails; 8 9 public Books() { 10 } 11 public Books(int bookId, String bookName, int bookCounts, String bookDetails) { 12 this.bookId = bookId; 13 this.bookName = bookName; 14 this.bookCounts = bookCounts; 15 this.bookDetails = bookDetails; 16 } 17 18 public int getBookId() { 19 return bookId; 20 } 21 22 public void setBookId(int bookId) { 23 this.bookId = bookId; 24 } 25 26 public String getBookName() { 27 return bookName; 28 } 29 30 public void setBookName(String bookName) { 31 this.bookName = bookName; 32 } 33 34 public int getBookCounts() { 35 return bookCounts; 36 } 37 38 public void setBookCounts(int bookCounts) { 39 this.bookCounts = bookCounts; 40 } 41 42 public String getBookDetails() { 43 return bookDetails; 44 } 45 46 public void setBookDetails(String bookDetails) { 47 this.bookDetails = bookDetails; 48 } 49 50 @Override 51 public String toString() { 52 return "Book{" + 53 "bookId=" + bookId + 54 ", bookName=‘" + bookName + ‘\‘‘ + 55 ", bookCounts=" + bookCounts + 56 ", bookDetails=‘" + bookDetails + ‘\‘‘ + 57 ‘}‘; 58 } 59 60 61 62 }
10.dao层接口及xml文件
1 package com.wu.dao; 2 3 import com.wu.pojo.Books; 4 import org.apache.ibatis.annotations.Param; 5 import org.springframework.stereotype.Repository; 6 7 import java.util.List; 8 9 @Repository 10 public interface BooksMapper { 11 12 int addBooks(Books books); 13 14 int delBooks(@Param("bookId") int id); 15 16 int updateBooks(Books books); 17 18 Books queryBooks(@Param("bookId") int id); 19 20 List<Books> queryAll(); 21 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 5 <mapper namespace="com.wu.dao.BooksMapper"> 6 <insert id="addBooks" parameterType="Books"> 7 insert into book (bookName,bookCounts,bookDetails) values (#{bookName},#{bookCounts},#{bookDetails}) 8 </insert> 9 10 <delete id="delBooks" parameterType="int"> 11 delete * from book where bookId=#{id} 12 </delete> 13 14 <update id="updateBooks" parameterType="Books"> 15 update book set bookName=#{bookName},bookCounts=#{bookCounts},bookDetails=#{bookDetails} 16 where bookId=#{bookId} 17 </update> 18 19 <select id="queryBooks" resultType="Books"> 20 select * from book where bookId=#{bookId} 21 </select> 22 23 <select id="queryAll" resultType="Books"> 24 select * from book 25 </select> 26 </mapper>
11.service层接口及实现类
1 package com.wu.service; 2 import com.wu.pojo.Books; 3 4 import java.util.List; 5 6 public interface BooksService { 7 8 int addBooks(Books books); 9 10 int delBooks(int id); 11 12 int updateBooks(Books books); 13 14 Books queryBooks(int id); 15 16 List<Books> queryAll(); 17 }
1 package com.wu.service; 2 3 import com.wu.dao.BooksMapper; 4 import com.wu.pojo.Books; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import java.util.List; 9 @Service 10 public class BooksServiceImpl implements BooksService { 11 12 @Autowired 13 private BooksMapper booksMapper; 14 15 public void setBooksMapper(BooksMapper booksMapper) { 16 this.booksMapper = booksMapper; 17 } 18 19 20 public int addBooks(Books books) { 21 return booksMapper.addBooks(books); 22 } 23 24 25 public int delBooks(int id) { 26 return booksMapper.delBooks(id); 27 } 28 29 30 public int updateBooks(Books books) { 31 return booksMapper.updateBooks(books); 32 } 33 34 35 public Books queryBooks(int id) { 36 return booksMapper.queryBooks(id); 37 } 38 39 40 public List<Books> queryAll() { 41 return booksMapper.queryAll(); 42 } 43 }
12.controller层
1 import org.springframework.web.bind.annotation.RequestMapping; 2 3 import java.util.List; 4 5 @Controller 6 @RequestMapping("/book") 7 public class BookController { 8 @Autowired 9 private BooksService booksService; 10 11 @RequestMapping("/queryAllBook") 12 public String queryAllBook(Model model){ 13 List<Books> books = booksService.queryAll(); 14 15 model.addAttribute("book",books); 16 17 return "allBooks"; 18 } 19 20 21 }
配置文件可分开配置便于理解,也可配置至一个文件中。
spring+springmvc+mybatis整合maven项目
原文:https://www.cnblogs.com/xiumu-zidiao/p/14719634.html