81,091
社区成员
发帖
与我相关
我的任务
分享
<?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="com.testForSSM.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.testForSSM.entity.User">
<constructor>
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
<arg column="nickname" jdbcType="VARCHAR" javaType="java.lang.String"/>
<arg column="state" jdbcType="INTEGER" javaType="java.lang.Integer"/>
</constructor>
</resultMap>
<insert id="insertUser" parameterType="com.testForSSM.entity.User">
INSERT INTO t_user (`nickname`,`state`) VALUE(#{nickname},#{state});
</insert>
<select id="selectUserById" parameterType="int" resultType="com.testForSSM.entity.User">
SELECT * FROM t_user WHERE id = #{id};
</select>
<select id="selectUserByName" parameterType="string" resultType="com.testForSSM.entity.User">
SELECT * FROM t_user WHERE nickname = #{nickname};
</select>
<select id="selectUserByState" parameterType="int" resultType="com.testForSSM.entity.User">
SELECT * FROM t_user WHERE state = #{state};
</select>
<select id="selectUserName" parameterType="int" resultType="string">
SELECT nickname FROM t_user WHERE id=#{id};
</select>
<select id="selectUserList" parameterType="string" resultType="com.testForSSM.entity.User">
SELECT * FROM t_user WHERE nickname=#{nickname};
</select>
</mapper>
package com.testForSSM.dao.impl;
import com.testForSSM.dao.UserDao;
import com.testForSSM.entity.User;
import com.testForSSM.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by admin on 2016/9/18.
*/
@Component("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private UserMapper userMapper;
@Override
public int insertUser(User user) {
System.out.println(user.toString());
userMapper.insertUser(user);
return 0;
}
@Override
public User selectUserById(int id) {
return userMapper.selectUserById(id);
}
@Override
public User selectUserByNickname(String nickname) {
return userMapper.selectUserByName(nickname);
}
@Override
public User selectUserByState(int state) {
return userMapper.selectUserByState(state);
}
@Override
public String selectUserName(int id) {
return userMapper.selectUserName(id);
}
@Override
public List<User> selectUserList(String nickname) {
return userMapper.selectUserList(nickname);
}
}
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!--配置 使得bean可以进行自动加载-->
<context:annotation-config/>
<!-- 注解开启 -->
<context:component-scan base-package="com.testForSSM"></context:component-scan>
<!--<context:component-scan base-package="com.testForSSM" use-default-filters="false">-->
<!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--<context:exclude-filter type="annotation"-->
<!--expression="org.springframework.web.bind.annotation.ControllerAdvice"/>-->
<!--</context:component-scan>-->
<!--配置自动扫描 @Aspect 标签-->
<aop:aspectj-autoproxy/>
<!-- aop 开启 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<import resource="spring-mybatis-res.xml"></import>
<!--<bean id="testHello" class="com.testForSSM.TestForAOPImpl"/>-->
<!--<bean id="testAOP" class="com.testForSSM.TestForAOPAspect"/>-->
</beans>
spring-mybatis-res.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<!-- 配置数据源 资源库 -->
<bean id="dataSourceTestOfMine" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${sysadmin.url}"/>
<property name="username" value="${sysadmin.username}"/>
<property name="password" value="${sysadmin.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="200"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat"/>
</bean>
<!-- 配置myBatis sqlSeesionFactory -->
<!--扫描XML文件的配置-->
<bean id="sqlSessionFactoryTestOfMine" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceTestOfMine"/>
<property name="mapperLocations" value="classpath*:com/testForSSM/mapper/UserMapper.xml"/>
<property name="configLocation" value="classpath:MyBatisConfig.xml"/>
<!--<property name="typeAliasesPackage" value="com.testForSSM.entity"/>-->
</bean>
<!-- 自动扫描配置,即mapper包下的所有接口都会通过SqlSessionFactory代理,不需要再手工去实现DAO接口 -->
<!-- 扫描接口的配置文件 -->
<bean id="mapperInterfaceTestOfMine" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.testForSSM.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryTestOfMine"/>
</bean>
<!-- 事务 -->
<bean id="transactionManagerTestOfMine" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceTestOfMine">
</property>
</bean>
<!--事务属性-->
<tx:advice id="txAdviceTestOfMine" transaction-manager="transactionManagerTestOfMine">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="count*" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="has*" propagation="REQUIRED" read-only="true"/>
<tx:method name="locate*" propagation="REQUIRED" read-only="true"/>
<tx:method name="list*" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--Aop管理事务-->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* *com.testForSSM.dao.*.*(..))" advice-ref="txAdviceTestOfMine"/>
</aop:config>
</beans>
ApplicationContext-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.testForSSM" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--添加default的servlet解析-->
<mvc:annotation-driven/>
</beans>