一:前言
这仅仅是个小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>***.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<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);
int deleteBook(int id);
int updateBook(Books books);
Books queryBookById(int id);
List<Books> queryBooks();
List<Books> queryBooksById(int id);
}
b:BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//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 books
values(#{id},#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBook" parameterType="_int">
delete from books
where 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);
int deleteBook(int id);
int updateBook(Books books);
Books queryBookById(int id);
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;
}
@Override
public int addBook(Books books) {
return bookMapper.addBook(books);
}
@Override
public int deleteBook(int id) {
return bookMapper.deleteBook(id);
}
@Override
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List<Books> queryBooks() {
return bookMapper.queryBooks();
}
@Override
public 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")
private BookService bookService;
@RequestMapping("/allBook")
public String listBook(Model model) {
List<Books> list = bookService.queryBooks();
model.addAttribute("list",list);
return "allBook";
}
@RequestMapping("/toAddBook")
public String toAddPaper() {
return "addBook";
}
@RequestMapping("/addBook")
public String addBooker(Books books) {
bookService.addBook(books);
return "redirect:/book/allBook";
}
@RequestMapping("/toUpdateBook")
public String toUpdateBook(Integer id, Model model) {
Books books = bookService.queryBookById(id);
model.addAttribute("Book",books);
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";
}
@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 configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.wyj.Pojo"/>
</typeAliases>
<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/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
|