SSM整合
程序员文章站
2022-04-25 16:21:16
...
1.项目pom.xml文件导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--单元测试的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--MyBatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--MySQL的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
<!--SpringMVC的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring连接数据库的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring配置切面的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--spring的上下文的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!--Spring和MyBatis和整合依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--C3p0的依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<!--servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
2.resources文件夹中新建applictioncntest-cont.xml与springmvc.xml和log4j.properties
applictioncntest-cont.xml;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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">
<!--配置数据源-->
<bean id="dateSuose" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="123"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ZLS?characterEncoding=utf-8"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="maxPoolSize" value="20"></property>
<property name="initialPoolSize" value="3"></property>
</bean>
<!--spring代理产生sqlSessionFactory-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dateSuose"></property>
<!--配置别名-->
<property name="typeAliasesPackage" value="com.zl.mode "></property>
<!--配置mode映射文件-->
<property name="mapperLocations" value="classpath:com/zl/mode/*.xml"></property>
</bean>
<!--配置mappen扫描接口-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zl.mappen" ></property>
</bean>
<!--配置事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSuose"></property>
</bean>
<!--配置切面-->
<tx:advice id="transactionInterceptor" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置切点-->
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.zl.services.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pointCut"></aop:advisor>
</aop:config>
</beans>
springmvc.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"
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">
<!--注解扫描-->
<context:component-scan base-package="com.zl"></context:component-scan>
<!--配置视图解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
log4j.properties:
# Global logging configuration
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# %时间 [%线程] %log级别(DEBUG,INFO,WARN,ERROR,FATAL,5代表格式化长度,不足用空格填补) %完整类名 -(自定义分隔符,也可以是':',随你) %消息%换行
# log4j.appender.A1.layout.ConversionPattern=%d [%t] %5p %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=%5p %d %c: %m%n
3.web.xml文件中打开spring管理和svlete核心跟过滤器等
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--加载spring的环境-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applictioncontest.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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring的核心监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
4.在java文件夹中新建com.ssm.controller、com.ssm.mappen、com.ssm.mode、com.ssm.services包
1.controller包中键入XX类:
package com.zl.controller;
import com.zl.mode.Zuoze;
import com.zl.services.ZuozeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class ZuozeController {
@Autowired
private ZuozeServices zuozeServices;
@RequestMapping("test")
public String test(){
List<Zuoze> li=zuozeServices.findAllZuoze();
for (Zuoze zuoze : li) {
System.out.println(zuoze);
}
return "index";
}
}
2.mode包中键入实体类与映射文件
3.mappen包中键入接口:
package com.zl.mappen;
import com.zl.mode.Zuoze;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface ZuozeMappen {
@Select("select * from zuoze") //这里直接是受用的注解方式
public List<Zuoze> findAllZuoze();
}
4.在servce包中新建接口和imp包(接口实现类)在实现类中调用mappne和接口方法完成事物与业逻辑
imp实现类:
package com.zl.services.imp;
import com.zl.mappen.ZuozeMappen;
import com.zl.mode.Zuoze;
import com.zl.services.ZuozeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.xml.ws.Action;
import java.util.List;
@Service("ZuozeServices")
public class ZuozeServicesImp implements ZuozeServices {
//创建mappen的引用
@Autowired
private ZuozeMappen zuozeMappen;
@Override
public List<Zuoze> findAllZuoze() {
return zuozeMappen.findAllZuoze();
}
}
上一篇: SSM的POM(1)
下一篇: Spring Data JPA 查询