springmvc-ssm整合小项目
1.依赖
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--Servlet-jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jsp-api-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.8</version>
</dependency>
</dependencies>
2.-----------静态资源过滤
<!--静态资源过滤-->
<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>
3.-----------配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--ServletDispatcher-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cn/com/scitc/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<!--解决编码乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
4.dao包
1.BookMapper---------->>>>>>>>>>>>>>
import cn.com.scitc.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//添加一本书
int addBook(Books books);
//根据id删除一本书
int deleteBookByid(@Param("bookID") int id);
//查找所有书籍
List<Books> findBooks();
//更新数据
int updateBook(Books books);
//根据id查找一本书
Books findByBookId(@Param("bookID") int id);
//根据姓名查一本书
List<Books> findByname(@Param("bookName") String name);
}
2.BookMapper.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="cn.com.scitc.dao.BookMapper">
<!--添加一本书-->
<insert id="addBook" parameterType="cn.com.scitc.pojo.Books">
insert into books(id,bookname,bookcounts,appearance)values
(#{bookID},#{bookName},#{bookCounts},#{detail})
</insert>
<!--根据id删除-->
<delete id="deleteBookByid" parameterType="int">
delete from books where id=#{bookID}
</delete>
<!--查找所有书籍-->
<select id="findBooks" resultMap="findAllmap">
select * from books
</select>
<!--更新书籍-->
<update id="updateBook" parameterType="cn.com.scitc.pojo.Books">
update books set bookname=#{bookName},bookcounts=#{bookCounts}
,appearance=#{detail} where id=#{bookID}
</update>
<!--根据id查找一本书-->
<select id="findByBookId" resultMap="findAllmap">
select * from books where id=#{bookID}
</select>
<!--关系映射-->
<resultMap id="findAllmap" type="cn.com.scitc.pojo.Books">
<result property="bookID" column="id"/>
<result property="detail" column="appearance"/>
</resultMap>
<!--根据名字查-->
<select id="findByname" resultMap="findAllmap" >
select * from books
where bookname like '%' #{bookName} '%';
</select>
</mapper>
3.Dao包的spring-dao.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置整合mybatis-->
<!--1关联数据库文件-->
<context:property-placeholder location="classpath:/cn/com/scitc/database.properties"/>
<!--2数据库连接池-->
<!--数据库连接池补充
dbcp:半自动化操作,不能自动连接。
c3p0:全自动化操作(自动的加载配置文件并设置到对象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置连接池属性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--c3p0私有属性-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时长-->
<property name="checkoutTimeout" value="10000"/>
<!--获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3.配置SqlsessionFactory对象-->
<bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置mybatis-config.xml全局文件-->
<property name="configLocation" value="classpath:/cn/com/scitc/mybatis-config.xml"/>
</bean>
<!--4配置扫描Dao接口包,动态实现Dao接口注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlsessionFactory-->
<!--
这里的需要设置关联的sqlsessionFactory对象的名称
然后找到对应的sqlsessionfactory动态的注入sqlsessionfactory
-->
<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"/>
<!--给出需要扫描的Dao接口包-->
<property name="basePackage" value="cn.com.scitc.dao"/>
</bean>
</beans>
5.配置service包
1.BookService接口类
package cn.com.scitc.service;
import cn.com.scitc.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.awt.print.Book;
import java.util.List;
//BookService底层实现调用Dao
public interface BookService {
//添加一本书
int addBook(Books books);
//根据id删除一本书
int deleteBookByid(@Param("bookID") int id);
//查找所有书籍
List<Books> findBooks();
//更新数据
int updateBook(Books books);
//根据id查找一本书
Books findByBookId(@Param("bookID") int id);
//根据姓名查一本书
List<Books> findByname(String name);
}
2.接口实现类
package cn.com.scitc.service;
import cn.com.scitc.dao.BookMapper;
import cn.com.scitc.pojo.Books;
import java.util.List;
//BookService的实现类
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
//调用Dao层接口,设置一个set入口,方便被spring管理。
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}
@Override
public int deleteBookByid(int id) {
return bookMapper.deleteBookByid(id);
}
@Override
public List<Books> findBooks() {
return bookMapper.findBooks();
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books findByBookId(int id) {
return bookMapper.findByBookId(id);
}
@Override
public List<Books> findByname(String name) {
return bookMapper.findByname(name);
}
}
3.service的spring-service.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描相关bean-->
<context:component-scan base-package="cn.com.scitc.service"/>
<!--把BookServiceImpl注入到IOC容器-->
<bean id="BookServiceImpl" class="cn.com.scitc.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
6.pojo包
1.Books模型类
package cn.com.scitc.pojo;
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
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 int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
7.controoler包
1.BookControoler
package cn.com.scitc.controller;
import cn.com.scitc.pojo.Books;
import cn.com.scitc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model){
List<Books> books = bookService.findBooks();
model.addAttribute("list", books);
return "allbook";
}
@RequestMapping("/toAddBook")
public String addPaper(Books book){
System.out.println(book);
bookService.addBook(book);
return "redirect:/book/allBook";
}
@RequestMapping("/toUpdateBook")
public String toUpdateBook(int id,Model model){
Books books = bookService.findByBookId(id);
model.addAttribute("book", books);
return "updatebook";
}
@RequestMapping("/updateBook")
public String updateBook(Books books){
System.out.println(books);
bookService.updateBook(books);
return "redirect:/book/allBook";
}
@RequestMapping("/del/{bookid}")
public String deleteBook(@PathVariable("bookid") int id){
bookService.deleteBookByid(id);
return "redirect:/book/allBook";
}
@RequestMapping("/findbyname")
public String findbyname(String findbyname,Model model){
System.out.println(findbyname);
if(findbyname==""){
List<Books> list= bookService.findBooks();
model.addAttribute("list", list);
return "allbook";
}else {
List<Books> books= bookService.findByname(findbyname);
model.addAttribute("list", books);
return "allbook";
}
}
}
2.springmvc的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置springMVC-->
<!--开启SpringMVC注解驱动-->
<mvc:annotation-driven/>
<!--静态资源servlet配置-->
<mvc:default-servlet-handler/>
<!--配置显示ViewResovler视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描web相关的bean-->
<context:component-scan base-package="cn.com.scitc.controller"/>
</beans>
mybatis-config配置
<?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>
<typeAliases>
<package name="cn.com.scitc.pojo"/>
</typeAliases>
<mappers>
<package name="cn.com.scitc.dao"/>
</mappers>
</configuration>
applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-mvc.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-dao.xml"/>
</beans>
db.properties配置
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/webapp1902?serverTimezone=GMT%2B8
jdbc.username=webapp1902
jdbc.password=7963158233

浙公网安备 33010602011771号