SSM整合
pom.xml
首先是依赖
<dependencies>
<!--====================================MyBatis===============================================-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version>
</dependency>
<!--LOG4J-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--数据库连接池:c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.1.2</version>
</dependency>
<!--===============================Spring&SpringMVC===========================================-->
<!--spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring切面-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--=================================Servlet,JSP====================================================-->
<!--Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!--JSTL-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--JSTL-api-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--=================================其他====================================================-->
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<!--JackSon-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
MyBatis与Spring的依赖支持表如下,如果出现不知名状的错误,请检查依赖版本是否互相支持
| MyBatis-Spring | MyBatis | Spring Framework | Spring Batch | Java |
|---|---|---|---|---|
| 3.0 | 3.5+ | 6.0+ | 5.0+ | Java 17+ |
| 2.1 | 3.5+ | 5.x | 4.x | Java 8+ |
然后解决Maven静态资源导出问题
<!--Maven静态资源导出问题解决-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
构建数据库
CREATE DATABASE ssm;
USE ssm;
CREATE TABLE `books`(
`bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID`(`bookID`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢')
项目结构
项目结构:Controller,Dao,pojo,Service
配置文件
- MyBatis的MyBatis-Config.xml文件
- Spring的ApplicantContext.xml文件
- Spring的从ApplicantContext.xml分理处的Spring-Dao.xml文件
- SpringMVC的SpringMVC-Servlet.xml文件
- 数据库连接配置的DB.properties文件
MyBatis-Config.xml:
由于大部分操作都交给了Spring操作,所以做一个别名配置即可
<?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="pojo"/>
</typeAliases>
</configuration>
ApplicantContext.xml
ApplicantContext文件的作用是,导入所有的Spring配置文件以做一个整合,之后所有导入Spring相关文件和创建Bean都使用ApplicantContext文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<import resource="Spring-Dao.xml"/>
<import resource="Spring-Service.xml"/>
<import resource="SpringMVC-Servlet.xml"/>
<!--↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓OtherBeans↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-->
</beans>
Spring-Dao.xml
Spring-Dao文件的主要配置有:配置数据库连接池(DataSource),配置SqlSessionFactory对象,配置SqlSession对象
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 使用该标签加载properties文件-->
<context:property-placeholder location="classpath:DB.properties"/>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.dirver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 也可以使用其他连接池,例如c3p0,但此处代码有问题,不要直接使用-->
<!-- <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<!-- <property name="driverClass" value="${jdbc.dirver}"/>-->
<!-- <property name="jdbcUrl" value="${jdbc.url}"/>-->
<!-- <property name="user" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- <property name="maxPoolSize" value="30"/>-->
<!-- <property name="minPoolSize" value="10"/>-->
<!-- <property name="autoCommitOnClose" value="false"/>-->
<!-- <property name="checkoutTimeout" value="10000"/>-->
<!-- <property name="acquireRetryAttempts" value="2"/>-->
<!-- </bean>-->
<!--SqlSessionFactory对象-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:Mybatis-Config.xml"/>
<property name="mapperLocations" value="classpath*:Dao/*.xml"/>
</bean>
<!--SqlSessionTemplate类实际上就是Spring提供的SqlSession对象-->
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--由于SqlSessionTemplate类没有SqlSessionFactory的set方法,所以只能通过构造函数注入-->
<constructor-arg index="0" ref="SqlSessionFactory"/>
</bean>
<!--MapperScannerConfigurer,可以自动生成扫描的包下接口的bean对象,并为其注入SqlSessionFactory对象-->
<!--相当于简化了手动创建接口实现类,再向里注入SqlSession对象的过程-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入SqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<!--要扫描的路径-->
<property name="basePackage" value="Dao"/>
</bean>
</beans>
SpringMVC-Servlet.xml
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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="Controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
jdbc.dirver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true;characterEncoding=utf8;useSSL=true
jdbc.username=root
jdbc.password=xsgg2333
#mysql8.0+需要配置时区
Spring-MyBatis
先实现Spring-MyBatis,再接入SpringMVC
实现Spring-MyBatis的步骤和在Spring时介绍的大差不差
pojo下的Books类
//lombok注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
Dao层
public interface BookMapper {
public int addBook(Books book);
public int deleteBook(Books book);
public int updateBook(Books book);
public Books getBook(int id);
public List<Books> getBooks();
}
<?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="Dao.BookMapper">
<insert id="addBook" parameterType="pojo.Books">
insert into books(bookName,bookCounts,detail)
values (#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBook" parameterType="pojo.Books">
delete from books
where bookID= #{bookID}
</delete>
<update id="updateBook" parameterType="pojo.Books" >
update book
set bookname=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID}
</update>
<select id="getBook" parameterType="int" resultType="pojo.Books">
select * from books
where bookID=#{id}
</select>
<select id="getBooks" resultType="pojo.Books">
select * from books
</select>
</mapper>
之前的操作是,创建一个BookMapper接口的实现类,并向其注入SqlSession对象,通过这个实现类来调用接口的方法。
这种方式Spring可以帮我们自动实现
<!--MapperScannerConfigurer,可以自动生成扫描的包下接口的bean对象,并为其注入SqlSessionFactory对象-->
<!--相当于简化了手动创建接口实现类,再向里注入SqlSession对象的过程-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入SqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<!--要扫描的路径-->
<property name="basePackage" value="Dao"/>
</bean>
在Spring-Dao.xml文件中,我们新加入了这个Bean,MapperScannerConfigurer的作用是扫描路径包下的接口,为其创建bean并注入SqlSession对象
Dao包下只有一个BookMapper接口,MapperScannerConfigurer就为我们创建了一个已经注入了SqlSession的bookMapper对象,和我们创建的实现类作用一样
Web框架
接入SpringMVC就要用到Web框架
点击模块,双击shift,搜索添加框架支持
添加完Web框架后,配置Web.xml文件,配置DispatcherServlet和CharacterEncodingFilter
<?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>dispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
SpringMVC
SpringMVC-Servlet.xml文件上面已经配置好了,下面配置一下Service层和Controller
在Servlet阶段时,Servlet调Service层,Service层调Dao层,这里也一样
Service层:
public interface BookService {
public int addBook(Books book);
public int deleteBook(Books book);
public int updateBook(Books book);
public Books getBook(int id);
public List<Books> getBooks();
}
public class BookServiceimpl implements BookService {
//包含一个BookMapper对象,通过它来调用对应的方法
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {this.bookMapper = bookMapper;}
@Override
public int addBook(Books book) {return bookMapper.addBook(book);}
@Override
public int deleteBook(Books book) {return bookMapper.deleteBook(book);}
@Override
public int updateBook(Books book) {return bookMapper.updateBook(book);}
@Override
public Books getBook(int id) {return bookMapper.getBook(id);}
@Override
public List<Books> getBooks() {return bookMapper.getBooks();}
}
然后我们新建一个Spring-Service.xml文件,用于创建Service层的bean
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描注解-->
<context:component-scan base-package="Service"/>
<bean id="bookServiceimpl" class="Service.BookServiceimpl" >
<!--这里注入的bookMapper对象,是上面MapperScannerConfigurer所扫描创建的bean-->
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
</beans>
然后来创建Controller
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier(value = "bookServiceimpl")
//BookService对象,值为上面创建的bean
private BookService bookService;
@RequestMapping("/allbooks")
public String getAllBooks(Model model) {
List<Books> books= bookService.getBooks();
model.addAttribute("books", books);
return "allbook";
}
}
至此已经配置完毕了,执行/book/allbooks请求后,就会跳转到该控制器,Model对象写代理books参数返回到allbook.jsp页面
错误合集
1.解决Maven静态资源导出问题,build-resources-resource里面不要有filter,有的话会导致静态文件依然无法导入
删掉filter后,使用maven的clean插件清除掉target文件夹,重新启动即可
2.tomcat工件启动错误/404
检查工件是否携带了外部库一起构建
项目结构-工件,检查你要运行的工件是否有lib文件夹,文件夹下是否携带了项目的所有jar包
3.Spring相关xml文件没有关联,导致ref的bean无法检测到
使Spring文件互相关联有两种方式
一是通过import标签,导入要使用的xml文件夹
二是在项目结构-模块中,添加Spring模块,选择Spring相关的文件,IDEA就会自动把他们连接起来

浙公网安备 33010602011771号