本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例
说明:1、本文暂未使用maven集成,jar包需要手动导入。
2、本文为基础教程,大神切勿见笑。
3、如果对您学习有帮助,欢迎各种转载,注明出处。
4、本文涉及源码和jar包下载地址:
一、导包
需要准备的包:
1、spring包
2、springmvc 包
3、mybatis 包
请自行下载导入,也可以去本人分享的网盘下载。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATkAAAB4CAIAAAAG8BLbAAAJ50lEQVR4nO2dPW/USByH/TVO0YlDAt3KV0RygfgK014VN9DtSkBxRCDRrIgtIhJdcVSIAq+u4EUsigDpZDeIPYmCQwKSBimVRZmScqUr7gq/zIzt8fp9xvZv9RS73lknYfbhPx57ftY83wUAqI8m/TcAABQBrgLQD+AqAP1gGK46M0MjC+m/BgAt0oqr73+/FFP8U7apaaYVv3TmE82YOoKXPCJXnZmh0Yfw4wD0gOZdPbpx4T/mcXTjQtHPLgijU6DZZLYK37VNTZ87gs/muRpvT/xfUAbUbSCf0q4ePjuw923Ru+/3L63PHq3PHn0/fXB2cvDt7zunr397v3+JRbjz1VSnclpEI8SMDXFmBvU2RSFXvQWBq6C/lHP14On9cxfPbZ3fEjV4u2+sv/+Tw9t9Q7x/RokF0UzLmU9Cu1ZTXSN22Mwi0ag2qrTBB+l2xnDuOVOZaWON7jlzO7MFo2ggjxKuBqLu7OzkuXrXWJ+9zmFx9aecHxHLaQcVdTXVAz1oSbQIrb2xisGAOVJuQaLn7PEqW5YtojHe0vai7airQD5FXY1FNU0zx9W/7hrrb3/GHO1u55Cxh1DOeMQbPqEHqwum7NHSmnCJczjYzh2scgfGTDPRdrgKFKCQq6yo+a4e7f6yPn0QcLS7vf72KJOzk4NsV32LaMSOy2lYYJmD1aROAZtdDWomHWAnnZzMVuLtcBUoQCFX7X176/xWjHVPOEPz9MaF9enB+vTgaHc7eJJGLKrrBXKahBuImoRQhRjlfNc2mbFuVDad+SRz7Mqc9UmNdTdsh6tAPg2fs1lc/XF9cudod3t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VUJ0Buvrm7ZvXX18Vadlt3tJwgKtSGKCr9kN77+He8ssyv1nneUvDAa5KYZiuLj+9uH1468mHJznN6uUtiZIc0nFKzHZugZtFtMlszqY0Be3jPUxmc8IsqVMIuCqFYbrq+e7yy/L63WvOu8eiZrXyljLW0IjjlNjtiUAWmtIUB75wksNVEDNYVz3fff7x2ZWbV14eZw+Ga+ctJYJURHFK/PZEXaWREZPZisuI8PzUS2WAq1IYrKsvjzfU1Xp5SwFs9BFcBe0yTFeXnzcfr9bKW8qIPhLEKZVyFWNgIGaYrhaZB66Xt5SOPsqLUyrsKpvrjbklwDFAVwueX206b6mFbEGMgQGDEq6CniL96zsq4CqojvSv76iAq6A60r++o0KmqwCA4sBVAPrB/xSQxnRrbjILAAAAAElFTkSuQmCC" alt="" />
在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完整的代码如下:
三、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
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPMAAACvCAIAAACn2H/kAAAQkUlEQVR4nO2d4W/TZh7H819MVTVV3UDriu6qWjqkSlOLKo2e++I6ygU1UAgqIaZ3TIL20OCmKK2XakX0NK6wrpuwAzvKjbJwsK3J6Oi5HKIMcQdliMDdpIRThdapTLC+QB7aKffCif3Yfuy4rRvbT37RR5X91I/9OHx4+jjx148vlUnaTjYrNAlCUBCmp6dPnjzdFBeyWWE1DgQARvhs2cudxCGZVCYZFYRgXIgLcSEbFYR4MCoEo3HHTxUoK2wwe/bTQznkNfvpwaa4MD09LQjBaLwpGm9qijY1BbFm99O+/IvmTQo5hvL5fDWMgKtOhTmn30TAhSzf7Pf7D7/ff/j9/sHn3/PPv+efZUce3zkydyXy78/2R+PxaFyIxqPRYFMwGmwKRqPRqG4P/bTKXZo1LOQYyldL1dRGOHQPXKTGB2YDBhQx+/CZQXaAxf/q7T8N/Pnr3I//0J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Ts2E0QhzOfOrHB18UZw8meurE2T4sxlonU5nlxRnNzUb3CVeQJOCM2e+1VyR66sTZg1iKaF0CYDTifRz7poYPv2JCydvDMZSq+4cPvL2OG+8bcQZk8mbQmgDAbIBMwGyATMBsgEzAbIBMwGyATMBsgEzAbIBMyDF75v7ExPXYxPVYYrJ34rqtE+d5NVRW1o8k9rzZU7dPJCa3jk22jk22Tt1tnbrbOvNt69Td1rHJetuOAmZ7EG+bLdl867ut6SdbZ7Lt6ScKdprtVcBsb3LifPOt7yShVWbfmmu9Ndc6xL/qeAudBsz2IBPXY1N3W1GhZ77Nj0MmbjaPTdZjh9rypBnquWCUO2DRW7Tz5drJNLTBylS+XH8HrPmkkqppI7lIDVILc/sKMn9Nkg1IEbiik1aC2R7k+Hh9+slWSej0zWapn5bkTlxtGOIbMLUw8xBIj9QpFCoBSnW5OqCgC1YapRaKTSqp3CuLxtsM78fK31auylWYT1oJZnuNieuxiZvN6Sft6WRzeqRh4mazxNTd1sTVhreHqw0qSt0hei2omQFaXlWXa/psTZTBMGlWdOo9dD4nXGitSPut7B/M9hQnzjenbzanRxrSIw1SPy1/HmKstYzUH0v/9s6ane+Gpckj1XWxgNlLwJNmT31Un2Yb0iMNY5P1x8frj4/XD/Gvvj1cjR+EyAhhhpeWVQbLs6Dj52LMFDPbbDRSzELMtJHqiKfuYRLqETmYbYj3zE5/1J5mG9IftU/dPiFhva7yGJ28zRxD+eiAcgFZ8GBJZhtlKK1MBKz6r6XeFc1mlNE8MrukXAXMNsNjZqfZhjTbkP5syKYdakYjdgAZSnfgQbNt0zppk9mQoXQjHjPbbmzqsyFD6T7K3GyAWP4P41/ae7ztNOYAAAAASUVORK5CYII=" alt="" />
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