概述
maven项目 SSM整合步骤(先前端、后后端)
首先创建一个maven项目在pom.mxl中添加war这样项目就能达成war包了。
在main新建webapp,如果pom.xml中不加war,webapp文件夹的颜色是不一样的,只有像我这样才行,然后创建WEB-INF,web.xml
整合ssm项目前先把需要的jar包全部导入
<?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>项目组名</groupId>
<artifactId>项目名</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
</dependencies>
</project>
然后再把前端整出来
前端(springMVC):
1.配置web环境
前端控制器配置
DispactherServlet映射
post的乱码处理
<?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">
<!-- 编码过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/ *</url-pattern>
<!--这里/ * 中间不用空格-->
</filter-mapping>
<!--前端控制器-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.mvc.xml配置
① 在resources目录下创建mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
② 扫描包
<context:component-scan base-package="com.qf.controller"></context:component-scan>
③开启注解
<mvc:annotation-driven></mvc:annotation-driven>
④配置静态资源的访问
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--由于mvc.xml中配置的前端控制器的映射路径为 / 与tomcat默认的静态资源访问路径相同,所以,静态资源路径会失效,所以要配置静态资源访问路径-->
⑤视图解析器配置
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3.编写UserController控制器
@Controller
@RequestMapping("/userController")
public class UserController {
@GetMapping("/selectAll")
public String selectAll() {
System.out.println("-------")
//前端能访问进来就行
return null;
}
然后把后端的整合进去
Mybatis配置
1.表和实体类的对应
2.编写UserDao接口和UserMapper文件(查询功能)
<mapper namespace="com.qf.dao.UserDao">
<!--namespace对应实现的接口名 id对应调用的方法名-->
<select id="selectAll" resultType="user">
SELECT * FROM t_users
</select>
</mapper>
3.通过Spring配置整合Mabatis配置:spring-context.xml
引入驱动配置文件
数据源采用druid
sqlSessionFactory的bean配置:引入数据源、别名、mapper配置
扫描dao层的map,创建UserMapper对象
quarte.properties 暂时不需要
#jdbc.properties 修改库名
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=xxxxx
jdbc.username=xxxx
jdbc.password=xxxxx
<!--spring-context.xml配置-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--这里使用的阿里的druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建sqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<list>
<value>classpath:com/qf/dao/UserDaoMapper.xml</value>
</list>
</property>
<!--为实体类起别名,在pom.xml中要添加build,(代码在下一段)-->
<property name="typeAliasesPackage" value="com.qf.entity"/>
<!--分页插件-->
<property name="plugins">
<bean class="com.github.pagehelper.PageInterceptor"></bean>
</property>
</bean>
<!--扫描dao层,自动生成对应的bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
Spring配置
1.Spring的测试环境
启动配置文件:
在test/java中创建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class UserTest {
@Autowired
private UserDao userDao;
@Test
public void test(){
System.out.println(userDao.selectAll());
}
}
成功后继续
2.service的配置
创建UserService的接口与实现类
实现类中要有@Service 和userDao自动注入
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<User> selectAll() {
return userDao.selectAll();
}
}
在spring-context.xml要加入注解扫描
<!--mvc.xml中扫描了controller包,spring-context.xml中在扫描的话可能会造成接口污染,所以在这里要声明不扫描controller包-->
<context:component-scan base-package="com.qf">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
加入事务管理器,管理和sqlSessionFactory的同一个数据源
注解的事务配置
<!--事务-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManger"/>
</beans>
在userServiceImpl中配置事务注解:
@Transactional(propagation = Propagation.SUPPORTS)
查询语句方法前加
SSM的整合
1.在controller中注入UserService
注意:需要加载spring-context.xml后才能识别
启动tomcat时,在web.xml中自动扫描spring层
<!-- 启动tomcat时,自动监听spring的配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
完整的目录结构,划掉的是一些整合用不到的
最后
以上就是搞怪黄豆为你收集整理的完整 maven项目 SSM整合步骤(先前端、后后端)的全部内容,希望文章能够帮你解决完整 maven项目 SSM整合步骤(先前端、后后端)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复