maven项目 集成SSM框架
Posted luopengsdok
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven项目 集成SSM框架相关的知识,希望对你有一定的参考价值。
1.新建maven项目
新建项目报错是因为缺少web.xml文件
右键项目名 找到“”java EE Tools“”
会自动生成一个web.xml文件
2.根据这模板创建文件
3.在pom.xml文件中添加
<!--集成spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--配置webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
4.在web.xml文件中添加以下数据
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>ssm1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下, 名称为[<servlet-name>]-servlet.xml,此处不定义,则为ssm1.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5.在springmvc-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="com.lianxi" />
<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/views/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!--释放静态资源 -->
<mvc:default-servlet-handler />
6.在webapp下创建index.jsp
<a href="testSpringMVC">测试springmvc</a><b/>
<a href="testSpring">测试spring</a><b/>
<a href="testMybatis">测试mybatis</a><b/>
7.在UserController中写测试(记得在加@Controller,不然扫不到)
@RequestMapping("/testSpringMVC")
public ModelAndView 测试springmvc是否正常()
System.out.println("这是springmvc测试");
return null;
8.启动项目访问http://localhost:8080/ssm/index.jsp
9.成功后在web.xml文件中添加
<!-- 加载spring核心文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<!--中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
10.在spring-context.xml文件中添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解自动生成bean -->
<context:component-scan base-package="com.lianxi" />
11.在UserService类中写测试(注记得加@Service注解)
public void 测试spring()
System.out.println("这是测试spring");
12.在UserController类中添加测试
@Autowired
UserService userService;
@RequestMapping("/testSpring")
public ModelAndView 测试spring是否正常()
System.out.println("这是spring测试");
userService.测试spring();
return null;
13.启动项目访问http://localhost:8080/ssm/index.jsp测试spring是否集成成功
14.成功后再pom.xml文件中添加
<!-- 集成mybatis -->
<!--mybatis,用来加载SqlSessionFactoryBean和MapperScannerConfigurer -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!--配置spring jdbc,用来加载DataSourceTransactionManager -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
15.在spring-context.xml文件中添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解自动生成bean -->
<context:component-scan base-package="com.lianxi" />
<!--加载外界文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="$jdbc.driver" />
<property name="url" value="$jdbc.url" />
<property name="username" value="$jdbc.user" />
<property name="password" value="$jdbc.password" />
</bean>
<!-- 读取mybatis文件 -->
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis和数据库连接 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!--读取mybatis的总配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 类型别名扫描包 -->
<property name="typeAliasesPackage" value="com.lianxi.pojo"></property>
<!--配置文件和接口分离 -->
<property name="mapperLocations" value="classpath:*/*.xml"></property>
</bean>
<!--mapper.xml -->
<!-- 配置接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.lianxi.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
16.在User类中添加
private String username;
private String password;
public String getUsername()
return username;
public void setUsername(String username)
this.username = username;
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
@Override
public String toString()
return "User [username=" + username + ", password=" + password + "]";
17.在UserDao接口中添加
User getUser(String username);
18.在UserDao.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.lianxi.dao.UserDao">
<select id="getUser" resultType="User">
select * from login_user where username=#username
</select>
</mapper>
19.在jdbc.properties文件中添加
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lianxi?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=root
20.在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>
<settings>
<!-- 驼峰法 只能是数据库是带下划线的时候可以匹配pojo不带下划线的字段 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
21.在数据库中建立login_user表 字段是username和password
22.在UserService类中添加
@Autowired
UserDao userDao;
public void 测试mybatis()
User user=userDao.getUser("张三");
System.out.println(user);
23.在UserController类中添加
@RequestMapping("/testMybatis")
public ModelAndView 测试mybatis是否正常()
System.out.println("这是Mybatis测试");
userService.测试mybatis();
return null;
24.启动项目访问http://localhost:8080/ssm/index.jsp测试mybatis是否集成成功
25.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>
<settings>
<!-- 驼峰法 只能是数据库是带下划线的时候可以匹配pojo不带下划线的字段 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
26.log4j.properties文件添加
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern =%-dyyyy-MM-dd HH:mm:ss,SSS %m%n
27.添加事务(spring-context.xml添加下列代码)
<!-- 数据源事务管理平台 可以管理不同的数据库 -->(注:对于修改增加等操作的Service层要添加@Transactional)
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入相应的数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解标签 -->
<tx:annotation-driven/>
28.补充pom.xml
<!--jstl、servlet-api、 -->
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Jackson Json处理工具包 springmvc 处理json数据的 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.2</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 日志包依赖 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>3.8.2</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin </artifactId>
<configuration>
<!-- 项目运行后使用的端口号 -->
<port>80</port>
<!-- 项目访问路径 -->
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
以上是关于maven项目 集成SSM框架的主要内容,如果未能解决你的问题,请参考以下文章