本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例
说明:1、本文暂未使用maven集成,jar包需要手动导入。
2、本文为基础教程,大神切勿见笑。
3、如果对您学习有帮助,欢迎各种转载,注明出处。
4、本文涉及源码和jar包下载地址:
一、导包
需要准备的包:
1、spring包
2、springmvc 包
3、mybatis 包
请自行下载导入,也可以去本人分享的网盘下载。
在lib目录创建spring,mybatis分类管理,将包复制进入各自的文件夹,并加载包。
二、配置文件
创建名为:config 的Source Folder文件夹。新建xml文件,命名为:springmvc.xml
2.1springmvc.xml 配置
2.1.1、加入需要的beans标签库(aop.tx.context.mvc)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">
2.1.2、开启扫描注解
<!-- 开启扫描注解 -->
<context:component-scan base-package="com.hyh.action,com.hyh.service.impl"></context:component-scan>
备注:com.hyh.action,com.hyh.service.impl包自己创建。
2.1.3、springmvc配置
<!-- 简化springmvc的配置 :代替了注解映射器和注解适配器 -->
<mvc:annotation-driven/>
2.1.4、实例化数据库连接池
<!-- 实例化数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="defaultAutoCommit" value="false"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
备注:次数使用MySQL 数据库,按实际自己配置。
2.1.5、实例化SqlSessionFactoryBean
<!-- 实例化SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis的配置文件<property name=""></property>-->
</bean>
2.1.6、实例化事务管理器对象
<!-- 实例化事务管理器对象 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池对象 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
2.1.7、声明事务
<!-- 声明事务:声明需要纳入事务管理的方法的特征 ,事务管理的切面-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="mod*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
2.1.8、织入:进行aop的配置
<!-- 织入:进行aop的配置 -->
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
2.1.9、配置Mapper,产生接口代理
<!-- 配置mybatis,Mapper接口的扫面,产生代理Mapper接口的代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入需要扫面的mapper接口的包的名字,多个包之间用逗号隔开 -->
<property name="basePackage" value="com.bjsxt.mapper"></property>
<!-- 注入sessionFactory,产生代理对象 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
以上springmvc.xml完整的代码如下:
View Code
三、web.xml配置
<!-- 配置sprngmvc的核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 在启动web容器的时候,实例化servlet对象 -->
<load-on-startup>1</load-on-startup>
<init-param>
<!-- 加载springmvc的配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
--------------------------------创建结束-------------------------------------
接下来是一个Demo ---------------------------------------------------------
四、创建包
com.hyh.action
com.hyh.mapper
com.hyh.service
com.hyh.service.impl
com.hyh.entity
1、实体类创建
private Integer uid;
private String uname;
private String address;
package com.hyh.entity;
public class Users {
private Integer uid;
private String uname;
private String address;
2、Mapper接口创建
saveusers(Users user);
package com.hyh.mapper;
import com.hyh.entity.Users;
public interface UsersMapper {
public void saveusers(Users user);
}
3、创建Mapper.xml文件
namespace:完全限定名一致
id:方法名一致;
parameterType:类名限定名;
<?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>
<!-- 定义数据库的增加操作 -->
<insert id="saveUsers" parameterType="com.hyh.entity.Users">
insert into users (uid,uname.address) values (null,#{uname},#{address})
</insert>
<!-- 其他模块操作 -->
</mapper>
4、service接口创建
saveUserService
package com.hyh.service;
import com.hyh.entity.Users;
public interface UsersService {
public void saveUsersService(Users user);
}
5、service接口实现类
纳入容器 @Service("service")
注入:@Resource
创建接口属性
@Service("userServiceImpl")
public class UsersServiceImpl implements UsersService {
@Resource
private UsersMapper usersMapper;
public void saveUsersService(Users user) {
// TODO Auto-generated method stub
usersMapper.saveusers(user);
}
}
6、action Handler类创建
@ConTroller
声明:@Resource
@RequestMapping("方法名保持一致")
@Controller("usersHandler")
public class UsersHandler {
@Resource(name = "usersServiceImpl")
private UsersService usersServiceImpl;
@RequestMapping("/saveUsers")
public String saveUsers(Users user) {
usersServiceImpl.saveUsersService(user);
return "success.jsp";
}
}
7、前台UI创建
(未完待续)附加知识:
五、拦截器配置
MyHandlerIntercepor.java
来源:https://www.cnblogs.com/hyhnet/p/5543082.html