SSM框架
- 一、注入依赖
- 二、配置web.xml
- 三、springmvc-common.xml配置
- 四、mybatis-config.xml配置
- 五、log4j.properties日志文件配置
- 六、jdbc.properties连接信息
- 七、applicationContext.xml配置
- 八、UserDao.xml案例
- 九、UserService接口
- 十、UserServiceImpl实现
- 十一、MyConverter
- 十二、UserController
一、注入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- spring的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.23.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.23.RELEASE</version>
</dependency>
<!-- 数据库驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- c3p0 -->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--jstl标签 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- mybatis的依赖 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- mybatis跟spring整合的依赖 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
二、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring-springMVC20221121</display-name>
<!-- 配置首页 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置过滤器。。解决post请求的中文乱码问题 :仅仅能解决post请求的中文乱码问题,注意get请求中文乱码无法处理-->
<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的主配置文件,方便监听器去加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置一个监听器 用来加载spring的主配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置核心分发器,加载springmvc的主配置文件 -->
<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-common.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三、springmvc-common.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:aop="http://www.springframework.org/schema/aop"
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
<!-- 开启IOC注解扫描 -->
<context:component-scan
base-package="com.wk"></context:component-scan>
<!-- 配置全局的类型转换器 时间格式转换 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.wk.convert.MyConverter"></bean>
</set>
</property>
</bean>
<!-- springmvc开启使用注解进行映射处理 -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean">
</mvc:annotation-driven>
<!-- 配置视图解析器:可以给返回的页面名称自动补充前缀和后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动补充前缀 -->
<property name="prefix" value="/"></property>
<!-- 自动补充后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 开启静态资源访问 location:资源文件所存放的物理路径 mapping:资源文件所访问的请求地址 -->
<!-- <mvc:resources location="/img/" mapping="/img/**"></mvc:resources>-->
<!-- <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>-->
<!-- <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>-->
</beans>
四、mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<!-- 新增mapper文件时一定要在myabtis的主配置文件中配置进来 -->
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml" /> -->
<mapper resource="com/wk/dao/UserDao.xml" />
</mappers>
</configuration>
五、log4j.properties日志文件配置
#LOG_DIR:/usr/local/.../logs\
LOG_DIR = E:\\logs
#Level:ERROR,WARN,INFO,DEBUG
log4j.rootLogger = INFO,Console,File
log4j.appender.Console = org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold = DEBUG
log4j.appender.Console.ImmediateFlush = true
log4j.appender.Console.Target = System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.File = org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.Threshold = INFO
log4j.appender.File.ImmediateFlush = true
log4j.appender.File.Append = true
log4j.appender.File.DatePattern = '_'yyyy-MM-dd'.log'
log4j.appender.File.encoding=UTF-8
log4j.appender.File.File = ${LOG_DIR}/info
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern = [%-5p][%d{ISO8601}]%m%n
六、jdbc.properties连接信息
根据个人连接数据库信息更改
a=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
b=root
c=123
d=com.mysql.cj.jdbc.Driver
七、applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.3.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx-4.3.xsd
"
>
<!-- 开启注解扫描 IOC -->
<context:component-scan base-package="com.wk"></context:component-scan>
<!-- 开启aop 注解
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-->
<!-- 3:去加载数据库连接信息的配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 2:注入数据源对象 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 用户名 -->
<property name="user" value="${b}"></property>
<!-- 密码 -->
<property name="password" value="${c}"></property>
<!-- 驱动类 -->
<property name="driverClass" value="${d}"></property>
<!-- 数据库连接地址 -->
<property name="jdbcUrl" value="${a}"></property>
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 扫描dao接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wk.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
<!--
spring集成事物管理:
1:注入事物管理着对象(需要引入数据源对象)
2:开启事物
3:指定在哪些方法上需要绑定事物
-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="tx"/>
</beans>
八、UserDao.xml案例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- nameSpace: 这个文件对应的dao接口是哪一个,注意写全称 -->
<mapper namespace="com.wk.dao.UserDao">
<insert id="addUser" parameterType="com.wk.entity.User">
insert into tb_user (uname,upwd,email,phone,addr,birth)
values(#{uname},#{upwd},#{email},#{phone},#{addr},#{birth})
</insert>
</mapper>
九、UserService接口
public interface UserService {
void insertUser(User user);
}
十、UserServiceImpl实现
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void insertUser(User user) {
userDao.addUser(user);
}
}
十一、MyConverter
public class MyConverter implements Converter<String, Date>{
@Override
public Date convert(String source) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
Date d=sdf.parse(source);
return d;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
十二、UserController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/reg")
public String reg(User user) {
userService.insertUser(user);
return "login";
}
}