1.需要到的jar包如下
2.项目层次如下
3.具体配置
3.1 spring的配置
3.1.1spring-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1配置数据库参数,引入刚才写的属性文件 -->
<!-- <context:property-placeholder location="classpath:resource.properties"/> -->
<!-- 2 数据库连接池 (c3p0)-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/project1102"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
<!-- c3p0私有属性 -->
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"></property>
<!-- 关闭后不自动提交 -->
<property name="autoCommitOnClose" value="false"></property>
<!-- 连接超时时间 -->
<property name="checkoutTimeout" value="1000"></property>
<!-- 连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"></property>
</bean>
<!-- 3需要使用mybatis则这里需要配置sqlsessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池
在单独的mybatis中是在configuration中配置的数据库连接,在这里mybatis值配置了mapper
才能获取sqlSessionFactory对象
所以需要为这个类注入数据库连接池
-->
<property name="dataSource" ref="dataSource"></property>
<!-- mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 扫描entity包使用别名,不然前面配置的mapper直接使用的类名会出错
多个包需要扫描 <property name="typeAliasesPackage" value="org.seckill.entity;org.seckill.entity2"/> -->
<!-- <property name="typeAliasesPackage" value="org.mtest.entity"></property> -->
<!-- 扫描sql配置文件也就是mapper里面的 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 4 配置扫描dao接口包,动态实现dao接口,自动注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入到sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 给出需要扫描的dao包 -->
<property name="basePackage" value="cn.weisoftware.ssm.dao"></property>
</bean>
</beans>
3.1.2 spring-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 扫描service包,创建service对象 -->
<context:component-scan base-package="cn.weisoftware.ssm.service"></context:component-scan>
<!-- 框架中的事务配置到service层,需要配置事务 -->
<!--配置声明式事务的处理 -->
<!--声明式事务的配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<!-- 配置事务传播特性 -->
<!--
transaction-manager="transactionManager"和上面的id对应
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!--具体的方法,做事务的处理
:事务的传播机制 :REQUIRED
propagation="REQUIRED" 默认添加的,可以不写
-->
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!--read-only="true" :只读事务,不做事务的提交处理 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="set*" propagation="REQUIRED" read-only="true"/>
<!-- <tx:method name="*" propagation="REQUIRED" read-only="true"/> -->
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 ,事务切面的配置-->
<aop:config>
<!--配置切入点 -->
<aop:pointcut expression="execution(* cn.weisoftware.ssm.service.*.*(..))" id="mycut"/>
<!--事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config>
</beans>
3.1.3 spring-web.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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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.xsd">
<!-- 扫描控制器,创建controller对象 -->
<context:component-scan base-package="cn.weisoftware.ssm.controller"></context:component-scan>
<!-- 使用注解配置这个-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置springmvc视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 扫描标注注解的对象 -->
<context:component-scan base-package="cn.weisoftware.controller"></context:component-scan>
<!-- 不过滤静态资源 -->
<mvc:default-servlet-handler/>
</beans>
3.2 mybatis-config.xml配置文件
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启全局缓存 -->
<setting name="cacheEnabled" value="true"></setting>
<!--配置日志输出sql信息 方便调试 -->
<setting name="logImpl" value="LOG4J"></setting>
<!--配置延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"></setting>
</settings>
<!-- <typeAliases> <typeAlias alias="User" type="com.yiibai.pojo.User" />
<typeAlias alias="Order" type="com.yiibai.pojo.Order" /> </typeAliases> -->
<!-- 为类设置别名 -->
<typeAliases>
<package name="cn.weisoftware.ssm.entity" />
</typeAliases>
<!-- Mybatis和Spring 集成之后,这些可以完全删除(注释掉),数据库连接的管理交给 Spring 来管理 -->
<!-- <environments default="development"> <environment id="development">
<transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver"
value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/yiibai?characterEncoding=utf8"
/> <property name="username" value="root"/> <property name="password" value=""/>
</dataSource> </environment> </environments> -->
<!-- xml映射文件的路径需要用/表示 --><!-- <mapper resource="com/zy/ssm/dao/UserMapper.xml" /> -->
<!-- <mapper resource="com/zy/ms/dao/OrdersMapper.xml"/> -->
</configuration>
3.3 日志log4j的配置文件
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
#log4j.logger.dao.BookDao=TRACE
log4j.logger.cn.weisoftware.ssm.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4.UserMapper.java和UserMapper.xml
4.1UserMapper.java
4.2UserMapper.xml
<?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 namespace="cn.weisoftware.ssm.dao.UserMapper" >
<resultMap id="BaseResultMap" type="cn.weisoftware.ssm.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="mobile" property="mobile" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, username, mobile
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="cn.weisoftware.ssm.entity.User" >
insert into user (id, username, mobile
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="cn.weisoftware.ssm.entity.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="mobile != null" >
mobile,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="mobile != null" >
#{mobile,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="cn.weisoftware.ssm.entity.User" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="mobile != null" >
mobile = #{mobile,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="cn.weisoftware.ssm.entity.User" >
update user
set username = #{username,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
5.UserServiceImpl.java
6.UserController.java