一:前言
这仅仅是个小demo,体验了一把SSM配置地狱,就简单的实现CRUD
二:相关技术
Mybatis+Spring+SpringMVC+Mysql+Maven+Tomcat
三:功能展示
1:查询
2:增加
3:删除
4:修改
四:上码
1:导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssmbuild</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--Junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency></dependencies></project>
2:Maven的资源过滤
zaipom.xml中进行添加
<!--资源过滤--><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:基本结构框架
(1):Dao层
a:BookMapper
package com.wyj.Dao;import com.wyj.Pojo.Books;import java.util.List;public interface BookMapper {//增加一本书int addBook(Books books);//根据id删除一个bookint deleteBook(int id);//更新bookint updateBook(Books books);//根据id查询 返回一个BookBooks queryBookById(int id);//查询全部Book 返回list集合List<Books> queryBooks();List<Books> queryBooksById(int id);}
b:BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wyj.Dao.BookMapper"><insert id="addBook" parameterType="books">insert into booksvalues(#{id},#{bookName},#{bookCounts},#{detail})</insert><delete id="deleteBook" parameterType="_int">delete from bookswhere id = #{id}</delete><update id="updateBook" parameterType="books">update books<set><if test="bookName != null">bookName = #{bookName},</if><if test="bookCounts != null">bookCounts = #{bookCounts},</if><if test="detail != null">detail = #{detail},</if></set>where id = #{id}</update><select id="queryBookById" parameterType="_int" resultType="books">select * from books where id = #{id}</select><select id="queryBooks" resultType="books">select * from books</select><select id="queryBooksById" parameterType="_int" resultType="books">select * from books where id = #{id}</select></mapper>
(2):PoJo
package com.wyj.Pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {private int id;private String bookName;private int bookCounts;private String detail;
}
(3):Service层
业务层 调用dao层完成实际的业务
a:bookService
package com.wyj.Service;import com.wyj.Pojo.Books;import java.util.List;public interface BookService {//增加一本书int addBook(Books books);//根据id删除一个bookint deleteBook(int id);//更新bookint updateBook(Books books);//根据id查询 返回一个BookBooks queryBookById(int id);//查询全部Book 返回list集合List<Books> queryBooks();List<Books> queryBooksById(int id);
}
b:bookServiceImpl
package com.wyj.Service;import com.wyj.Dao.BookMapper;
import com.wyj.Pojo.Books;import java.util.List;public class BookServiceImp implements BookService{private BookMapper bookMapper;public void setBookMapper(BookMapper bookMapper) {this.bookMapper = bookMapper;}@Overridepublic int addBook(Books books) {return bookMapper.addBook(books);}@Overridepublic int deleteBook(int id) {return bookMapper.deleteBook(id);}@Overridepublic int updateBook(Books books) {return bookMapper.updateBook(books);}@Overridepublic Books queryBookById(int id) {return bookMapper.queryBookById(id);}@Overridepublic List<Books> queryBooks() {return bookMapper.queryBooks();}@Overridepublic List<Books> queryBooksById(int id) {return bookMapper.queryBooksById(id);}
}
(4):Controller层
Servlet在这里填写 响应前端的请求
package com.wyj.Controller;import com.wyj.Pojo.Books;
import com.wyj.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller
@RequestMapping("/book")
public class BookController {@Autowired//这个是属性注入当中 自动装配 他仅仅限于如果这个属性是一个类的话@Qualifier("BookServiceImpl")//如果其注册的bean 有多个的话,其可以指定其中的一个beanprivate BookService bookService;@RequestMapping("/allBook")public String listBook(Model model) {List<Books> list = bookService.queryBooks();model.addAttribute("list",list);return "allBook";}//转发到addBook.jsp这个页面去添加书籍@RequestMapping("/toAddBook")public String toAddPaper() {return "addBook";}//当提交表单后 我们将这个页面重定向到 全部的书籍的页面//这里的参数 books 我们是从表单提交的时候(填写的参数)然后的话我们的提交方式//是post 所以参数看不见 ,但是我们仍可以从url中将参数获取到@RequestMapping("/addBook")public String addBooker(Books books) {bookService.addBook(books);return "redirect:/book/allBook";}//修改书籍的请求(我们点击修改的操作的时候,在哪一行点击修改 我们就能获取到哪一行的//id信息,即可获取到这本书books;(我们是在url一行中可以获取到 id ,然后这个id参数我们就可以获取到// <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.id}">修改</a>@RequestMapping("/toUpdateBook")public String toUpdateBook(Integer id, Model model) {Books books = bookService.queryBookById(id);model.addAttribute("Book",books);//我们向updateBook.jsp中的参数Book进行赋值return "updateBook";}//修改表单的提交 提交到这个请求中进行修改@RequestMapping("/updateBook")public String updateBook(Books books,Model model) {System.out.println(books);bookService.updateBook(books);System.out.println(books);Books books1 = bookService.queryBookById(books.getId());model.addAttribute("Book",books);return "redirect:/book/allBook";}//删除一个页面(这个参数id 我们从前端请求的jsp页面中的url 我们可以获取到id)@RequestMapping("/deleteBook")public String deleteBook(Integer id) {bookService.deleteBook(id);return "redirect:/book/allBook";}//根据书籍编号查询一本书籍的信息@RequestMapping("/CheckBookById")public String CheckBook(Integer id,Model model) {List<Books> list = bookService.queryBooksById(id);model.addAttribute("list",list);return "allBook";}}
4:配置文件
(1):连接数据库
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/smbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=
jdbc.password=
(2):Mybatis-Config的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><typeAliases><package name="com.wyj.Pojo"/></typeAliases>//每写完一个Mapper要在这里进行注册一下<mappers><mapper class="com.wyj.Dao.BookMapper"/></mappers></configuration>
(3):整合spring和mybatis
a:spring-dao.xml
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--配置整合Mybatis--><!--1.关联数据库文件--><context:property-placeholder location="classpath:database.properties"/><!--2.数据库连接池--><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"/><!-- 配置MyBaties全局配置文件:mybatis-config.xml --><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中在dao层中我们写完接口和配置文件后 还需要再写一个实现类(其中要手动引入sqlSessionFactory或者继承一个sqlSupport)然后在配置文件中注册bean,然后再测试中,CPX getBean ....现在我们可以通过配置文件省略掉这个实现类--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--注入sqlSessionFactory--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!--给出要扫描的Dao层接口包--><property name="basePackage" value="com.wyj.Dao"/></bean>
</beans>
b:spring-mvc.xml
我们用户发起的请求,以及响应我们用户的信息
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--扫描包:使指定的包下的注解有效--><context:component-scan base-package="com.wyj.Controller"/><!--静态资源默认servlet配置--><mvc:default-servlet-handler/><!--开启注解驱动--><mvc:annotation-driven/><!--配置视图解析器--><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></beans>
c:spring-service.xml
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--扫描service层相关的bean--><context:component-scan base-package="com.wyj.Service"/><!--BookServiceImpl注入到IOC容器中--><!--这里的ref = bookMapper ref是参考上下文 我们是将其中几个spring.xml联合在一块--><bean id="BookServiceImpl" class="com.wyj.Service.BookServiceImp"><property name="bookMapper" ref="bookMapper"/></bean><!--配置事务管理器--><bean id = "transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入数据库连接池--><property name="dataSource" ref="dataSource"/></bean>
</beans>
d:将三个spring.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="spring-dao.xml"/><import resource="spring-service.xml"/><import resource="spring-mvc.xml"/></beans>