SSM框架搭建步骤
来源:互联网 发布:阿里云服务器登录地址 编辑:程序博客网 时间:2024/06/01 18:45
基本架构
配置xml文件
applicationContext.xml
- 配置数据源连接对象DataSource接口。
- 配置sqlSessionFactory对象–用于生成sqlSession对象创建一次与数据库的会话。
- 配置spring的声明式事务Spring详解资料
- 面向切面AOP编程配置
- 扫描生成dao层bean对象和service层bean对象bean对象详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> <!-- 先配置DataSource接口,DataSource由C3P0来提供 --> <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <!-- mysql连接 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hotelsuppliesdatabase" /> <property name="user" value="root" /> <property name="password" value="***" /> --> <!-- oracle连接 --> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="user" value="**"/> <property name="password" value="****" /> <!-- 最大连接数 --> <property name="maxPoolSize" value="60" /> <!--最小连接数 --> <property name="minPoolSize" value="1" /> <!--初始化大小 --> <property name="initialPoolSize" value="1" /> <property name="maxIdleTime" value="60" /> <property name="checkoutTimeout" value="10000" /> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 配置spring的声明式事务 --> <!-- 事务管理的bean --> <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="*" propagation="REQUIRED" /><!-- 所有的方法全部加入到事务中 --> <tx:method name="add*" rollback-for="EXCEPTION" /> <tx:method name="get*" propagation="NEVER" /><!-- 不执行事务操作 --> <tx:method name="set*" propagation="NEVER" /><!-- 不执行事务操作 --> </tx:attributes> </tx:advice> <!-- AOP编程实现 --> <!--配置参与事务的类 --> <aop:config> <!-- 切入点的配置,哪些类或者哪些方法进入到切面中 --> <aop:pointcut expression="execution(* com.jinglin.hotel.service..*(..))" id="allMethods" /> <!-- 把切入点同事务整合在一起 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" /> </aop:config> <!-- 配置代理对象及sqlSession <bean id="userInfoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.jinglin.hotel.dao.imp.UserInfoMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> 配置service <bean id="userInfoService" class="com.jinglin.hotel.service.UserInfoService"></bean> --> <!--简化配置代理 --> <!-- 对dao层的扫描,实际就是批量生成dao层的bean对象 生成的规则:bean的id名实际就是dao层类或接口的首字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置扫描的包名 --> <property name="basePackage" value="com.jinglin.hotel.dao.imp"></property> <!--配置生成 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 扫描service层 --> <!-- service层扫描(批量生成bean对象) --> <context:component-scan base-package="com.jinglin.hotel.service" /></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> <!--类型别名 实体对象映射文件中类型别名--> <typeAliases> <typeAlias type="com.zt.model.UserInfo" alias="UserInfo"/> </typeAliases> <!--配置映射文件 --> <mappers> <mapper resource="com/zt/model/UserInfoMapper.xml" /> </mappers></configuration>
springmvc.xml
- 开启注解采用注解开发
- 配置视图解析器视图解析器
- 扫描controller层
<?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: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-4.0.xsd" default-autowire="byName"> <!-- 开启扫描包的注解 --> <context:annotation-config/> <!-- 用注解的方式 --> <mvc:annotation-driven> </mvc:annotation-driven> <!-- 视图解析的bean对象 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> <!-- Controller层也通过扫描的方式进入spring中 --> <context:component-scan base-package="com.zt.controller"/></beans>
web.xml
1. 配置springmvc前端控制器。
2. 配置监听器
3. 配置过滤器过post提交滤数据乱码
<?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>springmvc1</display-name> <!-- 配置sringmvc的前端控制器 --> <servlet> <servlet-name>springmvc2</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>springmvc2</servlet-name> <url-pattern>*.action</url-pattern><!--配置的访问路径,一定是按照这种格式写 --> </servlet-mapping> <!-- 配置监听器,随着web容器加载,那么applicationContext就加载到内存中 --> <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> <!-- 防止post提交表单数据的时候出现乱码 --> <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>
创建实体对象配置映射文件
实体对象:继承接口 Serializable开启序列化功能java对象序列化
package com.zt.model;import java.io.Serializable;/** * @author my *创建用户对象 继承接口 Serializable开启序列化功能 */public class UserInfo implements Serializable{ private Integer userId; private String userName; private String userPwd; private String card; private String job; private String del;}
dao层接口实现:
dao层接口
package com.zt.dao;import java.util.List;/** * @author my *增删改查方法 * @param <T> */public interface MIDao<T> { int addItem(T t);//添加数据 int updateItem(T t);//更新数据 int deleteItem(Object id);//删除数据 T getModel(Object id);//获取单条数据 List<T> getItems(T t);//查询数据}
接口继承:
package com.zt.dao.imp;import com.zt.dao.MIDao;import com.zt.model.UserInfo;/** * @author my *继承接口 */public interface UserInfoMapper extends MIDao<UserInfo> {}
配置映射文件:Mapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 加载命名空间 注意在mybatis-confige.xml中配置映射路径--><mapper namespace="com.zt.dao.imp.UserInfoMapper"> <!--增 --> <insert id="addItem" parameterType="UserInfo"> insert into userInfo(userName,userPwd,card,job) values(#{userName},#{userPwd},#{card},#{job}) </insert> <!-- 删 --> <delete id="deleteItem"> delete from userInfo where userId=#{userId} </delete> <!-- 改 --> <update id="updateItem" parameterType="UserInfo"> update userInfo <set> <if test="userName !=null and userName != '' "> userName=#{userName} </if> <if test="userPwd !=null and userPwd !='' "> userPwd=#{userPwd} </if> <if test="card !=null and card != '' "> card=#{card} </if> <if test="job !=null and job!= '' "> job =#{job} </if> </set> </update> <!--查 --> <!-- 定义重复使用sql代码 --> <sql id="selectColumn"> select * from userInfo where del='N' </sql> <!-- 单条查询 --> <select id="getModel" resultType="UserInfo"> <include refid="selectColumn"></include> and userId=#{userId} </select> <!-- 查询集 --> <resultMap type="UserInfo" id="userInfos"> <id column="userId" property="userId"/> <result column="userName" property="userName"/> <result column="userPwd" property="userPwd"/> <result column="card" property="card"/> <result column="job" property="job"/> <result column="del" property="del"/> </resultMap> <select id="getItems" parameterType="UserInfo" resultMap="userInfos"> <include refid="selectColumn"></include> <if test="userId !=null and userId != '' "> and userId=#{userId} </if> <if test="userName !=null and userName != '' "> and userName=#{userName} </if> <if test="userPwd !=null and userPwd !='' "> and userPwd=#{userPwd} </if> <if test="card !=null and card != '' "> and card like concat('%',#{card},'%') </if> <if test="job !=null and job!= '' "> and job like concat('%',#{job},'%') </if> </select></mapper>
阅读全文
0 0
- SSM框架搭建步骤
- SSM框架搭建步骤1
- #**IDEA**搭建**SSM**框架步骤
- IntelliJ IDEA-搭建SSM框架步骤
- ssm框架搭建(主要配置步骤)
- 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤
- SSM框架的搭建及项目开发的步骤
- SSM框架的搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- SSM框架搭建
- 初学搭建SSM框架
- MyEclipse2014搭建SSM框架
- ssm框架搭建
- Maven搭建SSM框架
- 基于tensorflow的线性回归模型
- vue钩子函数
- WebService学习总结(二)——WebService相关概念介绍
- spss、R语言、Python数据分析系列(7):python-adf单位根检验
- Android7.0 PowerManagerService亮灭屏分析(三)
- SSM框架搭建步骤
- webstorm 2016.2.3激活
- 使用Supervisor管理Linux进程
- Java LinkedHashMap
- 【面试题】剑指offer18--判断一个二叉树是否为另一个二叉树的子结构
- Jquery 事件委托在andriod正常在ios失效
- SAPUI5教程——框架简介以及应用实践
- 抽象类和接口区别
- TCP的3次握手和4次挥手