简单的整合ssm框架,实现数据库的数据写入功能(注册用户及数据校验),springCRUD基本都类似操作。
源码:https://github.com/HashMaven/peisong.git
目录结构
1、导入spring、mybatis、mysql必要jar包
2、配置web.xml文件
服务器启动时就加载Spring容器
<!-- 监听器 -->
<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>
3、配置springMVC核心控制器
4、创建springMVC配置文件
(默认路径在web-inf下 名称:“servletName”-servlet.xml)
扫描handler和service
<context:component-scan base-package="com.xtkj.handler,com.xtkj.service">
</context:component-scan>
5、整合mybatis
导包(mybatis必备包+整合spring的jar包+数据库驱动包)
创建mybatis主配置文件(mybatis-config.xml)
创建数据源
<bean id="datasourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库URL -->
<property name="url" value="jdbc:mysql://localhost:3306/MyBatisuseUnicode=true&characterEncoding=utf-8" />
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 数据库用户 -->
<property name="username" value="root" />
<!-- 密码 -->
<property name="password" value="12345678" />
</bean>
将mybatis交给spring管理(sqlSessionFactory)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasourse"></property>
<!-- 加载mybatis主配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
自动扫描mapper(dao)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
6、导入校验包
启动校验驱动
<mvc:annotation-driven/>
注入校验bean
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
在数据模型上编写校验规则
对数据模型做校验
完整的web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 监听器 -->
<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>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
完整的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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 扫描 handler & service-->
<context:component-scan base-package="com.spring.handler,com.spring.service"></context:component-scan>
<!-- 配置数据源 -->
<bean id="datasourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库URL -->
<property name="url" value="jdbc:mysql://localhost:3306/MyBatis?useUnicode=true&characterEncoding=utf-8" />
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 数据库用户 -->
<property name="username" value="root" />
<!-- 密码 -->
<property name="password" value="12345678" />
</bean>
<!-- 将mybatis交给spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasourse"></property>
<!-- 加载mybatis主配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 自动扫描mapper(dao) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 启动校验驱动 -->
<mvc:annotation-driven/>
<!-- 注入校验bean -->
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
</beans>
完整的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>
<!-- 允许JDBC支持生成的键 -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<!-- 配置数据类型的别名 -->
<typeAlias type="com.spring.pojo.User" alias="user"/>
</typeAliases>
</configuration>
7、创建pojo包实例User---写入dao包业务操作类UserDao.java & UserDao.xml---service包写入UserServiceInterface接口
---UserService实现接口,通过DAO访问数据库---handler包写入UserHandler控制器处理业务逻辑
以上都完成就可以优化整合了,把配置文件都放到config包下 并进行拆分整合
----优化配置步骤
删除spring主配置文件
移动springMVC主配置文件
创建数据源配置文件
配置文件集中管理
将springMVC主配置文件拆分
优化后的目录结构:
jdbc.properties: jdbc连接池
url=jdbc:mysql://localhost:3306/MyBatis?useUnicode=true&characterEncoding=utf-8
driver=com.mysql.jdbc.Driver
username=root
password=12345678
mybatis-config.xml:mybatis配置文件
<?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>
<!-- 允许JDBC支持生成的键 -->
<setting name="useGeneratedKeys" value="true"/>
</settings>
<typeAliases>
<!-- 配置数据类型的别名 -->
<typeAlias type="com.spring.pojo.User" alias="user"/>
</typeAliases>
</configuration>
springMVC-jsr303.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 启动校验驱动 -->
<mvc:annotation-driven/>
<!-- 注入校验bean -->
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
</beans>
springMVC-mybatis.xml:spring接管mybatis配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 引入外部数据源-->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库URL -->
<property name="url" value="${url}" />
<property name="driverClassName" value="${driver}"></property>
<!-- 数据库用户 -->
<property name="username" value="${username}" />
<!-- 密码 -->
<property name="password" value="${password}" />
</bean>
<!-- 将mybatis交给spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis主配置文件 -->
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
</bean>
<!-- 自动扫描mapper(dao) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
springMVC-servlet.xml:springMVC主配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 引入配置文件 -->
<import resource="classpath:config/springMVC-mybatis.xml"/>
<import resource="classpath:config/springMVC-jsr303.xml"/>
<import resource="classpath:config/springMVC-tx.xml"/>
<!-- 扫描 handler & service-->
<context:component-scan base-package="com.spring.handler,com.spring.service"></context:component-scan>
</beans>
springMVC-tx.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
欢迎技术交流及指正!qq:792653668