SSM整合步骤(详细)
发布时间
阅读量:
阅读量
文章目录
-
-
-
- 工具
-
-
-
SSM整合之整合mybatis
-
- 0.创建数据库
1.规划项目架构并导入必要的开发工具
2.制定并生成必要的配置文件
3.实现与数据库的连接
4.编写mybatis配置以定义事务支持
5.定义业务实体类
6.创建对应的业务接口
7.生成实现类XML配置文件
8.将已编译好的mapper映射到mybatis配置中进行管理
9.构建服务层业务逻辑
- 0.创建数据库
-
SSM整合方案涉及对Spring框架的整合
-
- 第10步:配置Spring Data Oracle(整合DAO层)
-
-
第11步:配置Spring Service(整合Service层)
-
在applicationContext.xml文件当中加入:
-
整合SSM框架以实现SpringMVC功能
- 在项目的Web架构中增加必要的支持
-
配置并维护web.xml文件
-
在applicationContext.xml文件中加入:
-
编写并完成spring-mvc.xml配置内容
-
开始编写JAVASCRIPT脚本及Controller层开发工作
工具
IDEA 2019 3.
mysql 5.7
tomcat 9.0
SSM整合之整合mybatis
0.建立数据库

1. 建立项目结构,导入依赖包
依赖包
<!-- 导入依赖:junit, mybatis,mybats-spring,spring-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</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>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<!-- 资源过滤-->
<build>
<!--Maven资源过滤设置-->
<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>
AI写代码
2. 创建配置文件
- mybatis的核心配置文件:mybatis-config.xml
- spring的核心配置文件:applicationContext.xml
//mybatis-config.xml 干净的包
<?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>
</configuration>
AI写代码
//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">
</beans>
AI写代码
自此项目的基本架构已经搭建好,准备开始写!!!
3. 关联数据库
创建database.properties
jdbc.driver=com.mysql.jdbc.Driver
#Mysql8.0以上,增加一个时区的配置,url &serverTimezone=Asia/shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild? useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
AI写代码
4. 编写mybatis配置文件
<!--配置数据源,交给spring去做-->
<typeAliases>
<package name="com.zengwei.pojo"/>
</typeAliases>
AI写代码
5.编写实体类
//Books
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
AI写代码
6. 编写对应的接口
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookId") int id);
//修改一本书
int updateBook(Books books);
//查询特定书籍或者所有书籍
Books queryBookById(@Param("bookId")int id);
List<Books> queryAllBook();
}
AI写代码
7.编写对应的实现类xml文件
<mapper namespace="com.zengwei.dao.BookMapper">
//注:针对addBook主键bookID不用特意写,会自动增加!!!
<insert id="addBook" parameterType="com.zengwei.pojo.Books">
insert into ssmbuild.books(bookName,bookCounts,detail)
values (#{bookName}, #{bookCounts}, #{detail})
</insert>
<delete id="deleteBookById" parameterType="int">
delete from ssmbuild.books where bookID=#{bookId}
</delete>
<update id="updateBook" parameterType="com.zengwei.pojo.Books">
update ssmbuild.books
set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
where bookID = #{bookID}
</update>
<select id="queryBookById" parameterType="int" resultType="com.zengwei.pojo.Books">
select * from ssmbuild.books
where bookID = #{bookId}
</select>
<select id="queryAllBook" resultType="com.zengwei.pojo.Books">
select * from ssmbuild.books
</select>
</mapper>
AI写代码
8. 将写好的mapper文件在mybatis配置文件当中注册
<?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>
<!--配置数据源,交给spring去做-->
<typeAliases>
<package name="com.zengwei.pojo"/>
</typeAliases>
//注:用class的前提是接口与对应的xml文件名字一样,否则用resource直接定位对应的xml文件
<mappers>
<mapper class="com.zengwei.dao.BookMapper"/>
</mappers>
</configuration>
AI写代码
9.编写业务层:service层
//编写业务层接口
import java.util.List;
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(int id);
//修改一本书
int updateBook(Books books);
//查询特定书籍或者所有书籍
Books queryBookById(int id);
List<Books> queryAllBook();
}
AI写代码
//编写业务层实现类
public class BookServiceImpl implements BookService {
//业务层调用dao层就对了
private BookMapper bookMapper;
//此处用set方法方便spring注入对象!!!
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
AI写代码
SSM整合之整合spring
10.创建spring-dao.xml配置文件(整合dao层)
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--关联数据库配置-->
<context:property-placeholder location="classpath:database.properties"/>
<!--连接池
1.c3p0:自动化操作
2.dbcp:半自动化操作
3.druid:略
-->
<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"/>
<!-- 获取连接超时时间 10s-->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSesionFactory" 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容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.zengwei.dao"/>
</bean>
</beans>
//步骤4主要是代替BookMapper接口的实现类BookMapperImpl.省得每个接口都要写一个实现类!!!
AI写代码
11.创建spring-service.xml文件(整合service层)
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描service相关的bean -->
<context:component-scan base-package="com.zengwei.service" />
<!--BookServiceImpl注入到IOC容器中-->
<bean id="BookServiceImpl" class="com.zengwei.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
AI写代码
12.在applicationContext.xml文件当中加入:
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
AI写代码
SSM整合之整合springMVC
13.在项目结构当中增加对web的支持
14.配置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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置绑定的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</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>
AI写代码
15 .在applicationContext.xml文件当中加入:
<import resource="spring-mvc.xml"/>
AI写代码
16.编写spring-mvc.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 https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<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>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.zengwei.controller" />
</beans>
AI写代码
17.开始写js代码以及controller层
待更…
全部评论 (0)
还没有任何评论哟~
