SSM整合配置
一、导入相关依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.0.2.RELEASE</spring.version> <mysql.version>8.0.16</mysql.version> <log4j.version>1.6.6</log4j.version> <log4j1.version>1.2.12</log4j1.version> <jackson.version>2.9.0</jackson.version> <c3p0.version>0.9.1.2</c3p0.version> <asp.version>1.8.6</asp.version> <mybatis.version>3.4.5</mybatis.version> <mybatis-spring>1.3.3</mybatis-spring> <junit.version>4.12</junit.version> <lombok.version>1.18.16</lombok.version> <upload.version>1.3.1</upload.version> <io.version>2.4</io.version> <servlet_version>3.0.1</servlet_version> <gson.version>2.8.5</gson.version> <json.version>1.2.75</json.version> <page.version>5.2.0</page.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${asp.version}</version> </dependency> <!-- 事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- jdbc模板 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- mvc json--> <!-- json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${page.version}</version> </dependency> <!-- 整合mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring}</version> </dependency> <!-- log4j start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j1.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${log4j.version}</version> </dependency> <!-- log4j end --> <!-- 单元测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 上传图片 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${upload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${io.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet_version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${json.version}</version> </dependency> </dependencies>
二、创建三层架构目录
在main目录下创建java和resources目录
三层架构存放在java目录下
-
controller
-
service
-
mapper
- model(存放实体类)
resources目录存放配置及xml
-
applicationContext.xml
-
jdbcConfig.properties
-
log4j.properties
-
springmvc.xml
- mybatis.xml
mapper的xml(编写sql使用)
2.1. 编写web.xml
在webapp目录(没有自行创建)下创建web.xml,配置拦截器
<!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的监听器 默认只加载WEB-INF下的applicationContext.xml文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置前端控制器 --> <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:springmvc.xml</param-value> </init-param> <!-- 服务器启动时创建 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置字符串编码 --> <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> </web-app>
三、编写配置类
-
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 开启注解扫描 只扫描spring和mybatis --> <context:component-scan base-package="com.hk" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder> <!-- spring 整合 mybatis--> <!-- 1.配置连接池 C3P0 --> <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}" /> </bean> <!-- 2.配置SqlSessionFactory工厂 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 实体类起别名 --> <property name="typeAliasesPackage" value="com.hk.model"></property> <!-- 指定mapper接口所对应的xml--> <property name="mapperLocations" value="classpath:com/hk/mapper/*.xml"></property> <!-- 指定Mybatis的核心配置文件 --> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> <!-- 3.配置接口AccountDao所在的包 映射扫描配置类 mvc配置 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 接口所在的包 --> <property name="basePackage" value="com.hk.mapper" /> <!-- 扫描工厂并注入sqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sessionFactory" /> </bean> <!-- 配置spring声明式事务管理 --> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 传入连接池对象 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知 --> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="ins*" /> <tx:method name="up*" /> <tx:method name="del*" /> <!-- 只读的事务 --> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置AOP增强 --> <aop:config> <aop:pointcut id="mypoint" expression="execution(* com.hk.service.impl.*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"></aop:advisor> </aop:config> </beans>
3.1. 编写jdbc.properties
1、oracle数据库
jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:XE/oracle
jdbc.username = wk123
jdbc.password = 123456
2、Mysql数据库
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 jdbc.username = root jdbc.password = 123456
四、编写springmvc.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/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"> <!-- 开始注解扫描,只扫描COntroller --> <context:component-scan base-package="com.hk"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置视图解析器 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 不拦截的静态资源 --> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/image/**" location="/image/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/pages/**" location="/pages/" /> <mvc:resources mapping="/jsp/**" location="/jsp/" /> <mvc:resources mapping="/layui/**" location="/layui/" /> <!-- 支持mvc注解 并将重定向后Url附带参数去掉 --> <mvc:annotation-driven ignore-default-model-on-redirect="true"></mvc:annotation-driven> <!-- Controller 跳转页面,不能跳转添加即可 --> <!-- <mvc:default-servlet-handler></mvc:default-servlet-handler>--> </beans>
五、编写mybatis.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"> <!-- Mybatis的主配置文件 --> <configuration> <!-- 打印sql --> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <plugins> <!-- 分页配置 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 --> <property name="param1" value="value1"/> </plugin> </plugins> </configuration>
六、编写log4j
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory= CONSOLE , Debug, LOGFILE, info warn error fatal
log4j.rootCategory=INFO,CONSOLE,
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# RollingFileAppender 代表文件 ConsoleAppender 代表控制台
#日志输出到控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n 输出内容 (%F:%L) 具体哪一行
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm:ss}] [%15.15t] ===> %m (%F:%L) \n
#日志输出到文件
#log4j.appender.LOGFILE=org.apache.log4j.FileAppender
#log4j.appender.LOGFILE.Encoding=UTF-8
#log4j.appender.LOGFILE.File=E:/A/log4j.log
#log4j.appender.LOGFILE.Append=true
#log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.LOGFILE.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm:ss}] [%15.15t] ===> %m
<?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类名 --> <mapper namespace="com.hk.mapper.IAccount"> <!-- 配置引用 减少重复代码 id=方法名 --> <sql id="findall"> select * from account </sql> <!-- 查询所有 resultType=返回的参数类型 --> <select id="findAll" resultType="account" > <include refid="findall"></include> </select> <!-- 添加账户 parameterType=接受的类名 --> <insert id="SaveAccount" parameterType="account" > insert into account(name,money) values (#{name},#{money}) </insert> </mapper>
至此整个SSM环境就搭建完成啦,快去启动使用吧,如有问题,欢迎在评论区下方留言!大家共同学习哈
无论风雨,和自己一决胜负吧