Web:使用maven整合SSM框架
- 概述:
- 1.首先来看项目的目录结构
- 2.然后在java包下创建子包
- 3.接着完善resources包:
- 4.Web下的目录结构
- 5.配置maven依赖 **pom.xml**(运行环境是 Tomcat8.5,Spring4.0,jdk1.8)
- 6.配置Spring-*.xml
- 7.数据库中创建相应的表
- 8.创建entity类
- 9.创建Dao接口(数据交互)
- 10.在resources包的mapper子包下创建 **AppointmentMapper.xml**和**BookMapper.xml**
- 11.Service层相关。
- 12.Controller层相关实现
概述:
学完了SSM之后,跟着GitHub上的一篇博客做了下整合,可能因为是eclipse的或者版本的问题,期间出现了不少问题。然后记录了一下自己在idea上面搭建好的SSM整合方案。
原博客地址:
1.首先来看项目的目录结构
一、创建项目的大致文件结构
这里注意几点:
1、如果使用maven的骨架创建webapp,需要自己在main目录下新建java包和resources包,并且需要把java包和resources包都标记为源文件目录。(java包下放一些源代码文件,resources包下放一些配置文件)
2.然后在java包下创建子包
(其中dto和exception其实可以不要)这里每个包的用途应该很显然吧。
controller包:放的是一些SpringMVC下的控制类。@Controller修饰的类
dao包:放的基本是一些与数据打交道的类,与数据库、缓存相关的。
dto包:主要是一些处理web层与service层数据传输的类。
entity包:这个就是你的实体类bean啊什么的,都放在这里。
exception包:这个就是放一些自定义的错误处理的类。
service包:这差不多就是Spring相关的一些类吧,主要是用来写我们的业务逻辑。@Service修饰的类。
3.接着完善resources包:
mapper包:mybatis中的一些sql映射的实现(xxxMapper.xml)。
spring包:主要是spring和springmvc的配置文件,这里为了方便管理分成了三个,建议写两个(spring.xml和springMvc.xml)。
spring-dao.xml:主要处理数据源(就是整合mybatis的配置)相关的配置,主要三部分:数据库连接池,注册SqlSessionFactory,扫描所有mapper文件。
spring-service.xml:扫描所有@Service注解,注册TransactionManager,开启基于注解的声明式事务。
spring-web.xml:这里配置web相关的,配置支持静态资源加载;开启注解模式;配置视图解析器;扫描所有@Controller注解。
jdbc.properties是数据库连接的配置文件,log4j是日志的配置文件,然后还需配置mybatis-conf.xml就是mybatis的全局配置文件。
4.Web下的目录结构
这里resources下的一些包主要是规范,这里没怎么用到,WEB-INF下配置一个jsp文件夹用来存放jsp文件,WEB-INF目录下的文件由于安全性是无法直接访问到的。
5.配置maven依赖 pom.xml(运行环境是 Tomcat8.5,Spring4.0,jdk1.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soecode.ssm</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://github.com/liyifeng1994/ssm</url>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- 1.日志 -->
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 2.数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- DAO: MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 3.Servlet controller -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 4.Spring -->
<!-- 1)Spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!-- 2)Spring DAO层 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- 3)Spring controller -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- 4)Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- redis客户端:Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
<!-- Map工具类 -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
</build>
</project>
6.配置Spring-*.xml
jdbc.properties
这里有个地方要注意,命名不能直接使用username什么的,要用jdbc.username,直接使用username可能会与系统变量产生冲突。
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/XXX?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password={your password}
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"
xmlns:mvc="http://mybatis.org/schema/mybatis-spring" xmlns:aop="http://www.springframework.org/schema/aop"
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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1、注册外部的数据库相关参数设置文件 -->
<context:property-placeholder location="classpath:jdbc.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"/>
<property name="initialPoolSize" value="10"/>
<!-- 关闭连接池后不自动提交 -->
<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"/>
<!-- 配置所有 mapper 文件的地址 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
<!-- 设置要起别名(类小写)的包 -->
<property name="typeAliasesPackage" value="com.lnquan.entity"/>
<!-- 配置 mybatis 全局配置文件的路径-->
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
</bean>
<!-- !!! 一定要配置这个属性,表示使用CGLib进行代理。不然无法使用基于类的代理-->
<aop:config proxy-target-class="true"/>
<!-- 4、配置扫描 dao 接口的包,扫描所有的mapper接口的实现,使他们能够程序加载时自动注入到spring容器中-->
<mvc:scan base-package="com.lnquan.dao"/>
</beans>
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"
xmlns:mvc="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service包下所有使用@Service注解的类型-->
<context:component-scan base-package="com.lnquan.service"/>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-web.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--
1、开启 annotation-driven(SpringMVC注解模式)
自动注册
DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter等
-->
<!-- 2、使SpringMVC支持静态资源加载 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 3、配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<!-- 4、扫描web(就是扫描所有的controller)相关的bean -->
<context:component-scan base-package="com.lnquan.controller"/>
<context:component-scan base-package="com.lnquan.dao"/>
<context:component-scan base-package="com.lnquan.service.impl"/>
</beans>
7.数据库中创建相应的表
book表、appointment表:
数据表中各字段类型:
8.创建entity类
在entity包下添加以下两个类(自行添加有参、无参构造器,get和set方法,toString方法。这里就省略了)
Book:
public class Book {
private int bookId;
private String name;
private int number;
}
Appointment:
public class Appointment {
private int bookId;
private int studentId;
private Date appointTime;
private Book book;
}
9.创建Dao接口(数据交互)
主要是配置对book类和appointment类的一些操作。Dao层的实现主要是在mybatis中实现,但是需要我们在mapper配置文件中写相关的sql语句。
bookDao:
@Component
public interface BookDao {
//通过数的Id进行查询,返回Book
Book queryById(int id);
//查询所有的书
List<Book> queryAll();
//减少指定Id的书的库存,一次减少一本
int reduceNumber(int bookId);
}
appointmentDao:
@Component
public interface AppointmentDao {
//插入预约,返回预约号
int insertAppointment(@Param("bookId") int bookId,@Param("stuId") int stuId);
//根据主键查询学生的预约情况,携带预约的书籍
Appointment queryByKeyWithBook(@Param("bookId") int bookId,@Param("stuId") int stuId);
}
10.在resources包的mapper子包下创建 AppointmentMapper.xml和BookMapper.xml
进行如下配置。
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="com.lnquan.dao.BookDao">
<!--
Book queryById(int id);
List<Book> queryAll();
int reduceNumber(int bookId);
-->
<select id="queryById" resultType="book">
SELECT * FROM book WHERE book_id=#{id}
</select>
<select id="queryAll" resultType="book">
SELECT * FROM book ORDER BY book_id
</select>
<update id="reduceNumber">
UPDATE book SET number=number-1 WHERE book_id=#{bookId} AND number>0
</update>
</mapper>
AppointmentMapper.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="com.lnquan.dao.AppointmentDao">
<!--
//插入预约,返回预约号
int insertAppointment(@Param("bookId") int bookId,@Param("stuId") int stuId);
//根据主键查询学生的预约情况,携带预约的书籍
Appointment queryByKeyWithBook(@Param("bookId") int bookId,@Param("stuId") int stuId);
-->
<insert id="insertAppointment" useGeneratedKeys="true" keyColumn="book_id">
INSERT INTO appointment(book_id, student_id) VALUES (#{bookId},#{stuId})
</insert>
<resultMap id="apo" type="appointment">
<id property="studentId" column="stuID"/>
<result property="bookId" column="bookId"/>
<result property="appointTime" column="time"/>
<!--
这里因为在appointment类中携带book,数据库返回结果时需要使用联合标签(association)对book进行封装
-->
<association property="book" javaType="book">
<id property="bookId" column="bookId"/>
<result property="name" column="name"/>
<result property="number" column="number"/>
</association>
</resultMap>
<!--使用自定义的resultMap-->
<select id="queryByKeyWithBook" resultMap="apo">
SELECT a.student_id stuId,a.appoint_time time,a.book_id bookId,b.name name,b.number number
FROM appointment a JOIN book b ON a.book_id=b.book_id
WHERE a.student_id=#{stuId} AND a.book_id=#{bookId}
</select>
</mapper>
11.Service层相关。
在service包下创建BookService接口和AppointmentService接口。
BookService:
public interface BookService {
Book selectOneById(int bookId);
List<Book> selectAll();
int reduceNumber(int bookId);
}
AppointmentService:
public interface AppointmentService {
int addAppointment(int bookId,int stuId);
Appointment selectAppointment(int bookId,int stuId);
}
再在service包下创建一个impl子包,用来放Serviece接口的相关实现类。impl包下创建AppointmentServiceImpl 和 BookServiceImpl。
AppointmentServiceImpl :
@Service("AppointmentServiceImpl")
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
private AppointmentDao appointmentDao;
public int addAppointment(int bookId, int stuId) {
return appointmentDao.insertAppointment(bookId,stuId);
}
public Appointment selectAppointment(int bookId, int stuId) {
return appointmentDao.queryByKeyWithBook(bookId,stuId);
}
}
BookServiceImpl:
@Service("BookServiceImpl")
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public Book selectOneById(int bookId) {
return bookDao.queryById(bookId);
}
public List<Book> selectAll() {
return bookDao.queryAll();
}
public int reduceNumber(int bookId) {
return bookDao.reduceNumber(bookId);
}
}
12.Controller层相关实现
在controller包下创建AppointmentController和BookController两个控制类。
BookController:
@Controller
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookServiceImpl bookService;
@Autowired
@Qualifier("AppointmentServiceImpl")
private AppointmentServiceImpl appointmentServie;
@RequestMapping("/selectOne")
public String selectOne(@RequestParam("bookId") int bookId, Map<String , List<Book>> map){
Book book = bookService.selectOneById(bookId);
List<Book> books = new ArrayList<Book>();
books.add(book);
map.put("books",books);
return "list";
}
@RequestMapping("/selectAll")
public String selectAll(Map<String ,List<Book>> map){
List<Book> books = bookService.selectAll();
map.put("books",books);
return "list";
}
}
AppointmentController:
@Controller
public class AppointmentController {
@Autowired
@Qualifier("BookServiceImpl")
private BookServiceImpl bookService;
@Autowired
@Qualifier("AppointmentServiceImpl")
private AppointmentServiceImpl appointmentService;
@ResponseBody
@RequestMapping("addAppoint")
public Book addAppoint(@RequestParam("bookId") int bookId,
@RequestParam("stuId") int stuId){
bookService.reduceNumber(bookId);
appointmentService.addAppointment(bookId,stuId);
return bookService.selectOneById(bookId);
}
}
到这里基本后台的就写完了,可以自己试着写以下前端相关的。