67,512
社区成员
发帖
与我相关
我的任务
分享
<!-- 开启Autowired、Common、Persistence及Required标注的支持 -->
<context:annotation-config />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="defaultAutoCommit" value="false" />
<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
<property name="minEvictableIdleTimeMillis" value="3600000"/>
</bean>
<!-- MyBatis integration with Spring as define sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-configuration.xml" />
<!--<property name="mapperLocations" value="classpath*:cn/git/mis/common/model/mapper/*.xml" />-->
</bean>
<!-- 采用扫描指定包的mapper接口,而不是一一定义每个mapper -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.git.mis.common.model.mapper" />
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
</bean>
<!-- transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<configuration>
<typeAliases>
<typeAlias alias="User" type="cn.git.mis.common.model.bean.User" />
</typeAliases>
</configuration>
<mapper namespace="cn.git.mis.common.model.mapper.UserMapper">
<insert id="insertUser" parameterType="User" useGeneratedKeys="false" keyProperty="id">
insert into T_USER(id,gender,username,password) values(SEQ_USER_ID.nextval,#{gender},#{username},#{password})
</insert>
<select id="getUser" resultType="User" parameterType="map">
select * from T_USER where username = #{username} and password = #{password}
</select>
<select id="getUserById" resultType="User" parameterType="java.lang.Long">
select * from T_USER where id=#{id}
</select>
<update id="updateUser" parameterType="User">
update T_USER set gender=#{gender}, username = #{username} where id = #{id}
</update>
<update id="updateUserPwd" parameterType="map">
update T_USER set password=#{password} where id = #{id}
</update>
<delete id="deleteUserById">
delete from T_USER where id = #{id}
</delete>
</mapper>
public class MyBatisTest {
private static ApplicationContext xmlAppCtx = null;
static {
xmlAppCtx = new ClassPathXmlApplicationContext("classpath:spring/appctx-common.xml");
}
@Test
public void userTest() {
// Query
Map<String, Object> param = new HashMap<String, Object>();
param.put("username", "Langhia");
param.put("password", "123456");
User _user = mapper.getUser(param);
Assert.assertNotNull(_user);
}
}