81,092
社区成员
发帖
与我相关
我的任务
分享
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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"
default-autowire="byName">
<!-- 向spring注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor -->
<context:annotation-config></context:annotation-config>
<!-- 定义组件扫描的包名 -->
<context:component-scan base-package="edu.zjut" />
<!-- 导入属性配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mysql.properties</value>
</list>
</property>
</bean>
<!-- context:property-placeholder location="classpath:mysql.properties" / -->
<!-- 定义使用C3P0连接池的数据源 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的JDBC驱动 -->
<property name="driverClass">
<value>${jdbc.driverClassName}</value>
</property>
<!-- 连接数据库所用的URL -->
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<!-- 连接数据库的用户名 -->
<property name="user">
<value>${jdbc.user}</value>
</property>
<!-- 连接数据库的密码 -->
<property name="password">
<value>${jdbc.password}</value>
</property>
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxPoolSize">
<value>${jdbc.maxPoolSize}</value>
</property>
<!-- 设置数据库连接池的最小连接数 -->
<property name="minPoolSize">
<value>${jdbc.minPoolSize}</value>
</property>
<!-- 设置数据库连接池的初始化连接数 -->
<property name="initialPoolSize">
<value>${jdbc.initialPoolSize}</value>
</property>
<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
<property name="maxIdleTime">
<value>${jdbc.maxIdleTime}</value>
</property>
</bean>
<!-- 集成myBaits框架,配置sqlSessionFatory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- mybatis自动扫描包下的mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
<!-- optional unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="config.mybatis.mapper" />
</bean -->
<!-- 数据库的事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
<?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="User">
<!--
<resultMap type="edu.zjut.user.entity.User" id="userResultMap">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="password" column="password" />
</resultMap>
-->
<insert id="save" parameterType="edu.zjut.user.entity.User">
insert into user values(#{userName},#{password})
</insert>
<update id="update" parameterType="edu.zjut.user.entity.User">
update user set userName=#{userName},password=#{password}
where id=#{id}
</update>
<delete id="delete" parameterType="edu.zjut.user.entity.User">
delete from user where id=#{id}
</delete>
<select id="query" parameterType="edu.zjut.user.entity.User"
resultType="edu.zjut.user.entity.User">
select * from user where id=#{id}
</select>
<select id="queryPage" parameterType="edu.zjut.user.entity.User" resultType="edu.zjut.user.entity.User">
<![CDATA[
select * from user
where username <> #{userName} limit #{start},#{pageSize}
]]>
</select>
<select id="queryTotalCount" parameterType="edu.zjut.user.entity.User" resultType="Integer">
<![CDATA[
select count(*) from user
where username <> #{userName}
]]>
</select>
<select id="login" parameterType="edu.zjut.user.entity.User"
resultType="edu.zjut.user.entity.User">
select * from user where userName=#{userName} and
password=#{password}
</select>
</mapper>