driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=LF
password=LF
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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" />
<!-- 配置包扫描 -->
<context:component-scan base-package="cn.zr"/>
<!-- 启用自动代理 -->
<aop:aspectj-autoproxy/>
<!-- 配置事务传播属性 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有已get开头的方法都是只读,换句话说,get开始的方法不参与事务
-->
<tx:method name="get*" read-only="true" />
<!-- 其他方法使用默认配置 -->
<tx:method name="*" rollback-for="java.lang.Throwable" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* cn.zr.pringandmybatis.service.impl.*.*(..))" id="pointcut"/>
<!-- 将通知(事务属性)与切点连接一起 -->
<aop:advisor advice-ref="advice" pointcut-ref="pointcut" />
</aop:config>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置mybatis配置文件的路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置要映射类包的路径 -->
<property name="typeAliasesPackage" value="cn.zr.pringandmybatis.pojo"/>
<property name="mapperLocations">
<list>
<value>classpath:cn/zr/pringandmybatis/mapper/xml/*Mapper.xml</value>
</list>
</property>
</bean>
<!-- MyBatis扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定对应的接口跟Maper.xml文件映射所在的包 -->
<property name="basePackage" value="cn.zr.pringandmybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
<?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.zr.pringandmybatis.mapper.UserMapper">
<!-- 配置数据库字段与实体类属性的对应关系 -->
<resultMap type="User" id="userinfo">
<id column="username" property="name" />
<result column="age" property="age"/>
</resultMap>
<!-- 查询所有用户的信息 -->
<select id="getAllUserInfo" resultMap="userinfo">
select username,age from user
</select>
<!-- 添加用户 -->
<insert id="addUser" parameterType="User">
insert into user (username,age) values(#{name},#{age})
</insert>
<!-- 更新用户 -->
<update id="updateUser" parameterType="User">
update user set age = #{age} where username=#{name}
</update>
<delete id="deleteUser" parameterType="java.lang.String">
delete from user where username=#{name}
</delete>
</mapper>
package cn.zr.pringandmybatis.pojo;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
package cn.zr.pringandmybatis.mapper;
import java.util.List;
import org.springframework.stereotype.Component;
import cn.zr.pringandmybatis.pojo.User;
@Component
public interface UserMapper {
/**
* 获取所有用户的信息
* @return 用户对象的集合
*/
public List<User> getAllUserInfo();
/**
* 添加用户
* @param user 被添加的用户
* @return 返回成功操作数据库的数据量
*/
public int addUser(User user);
/**
* 更新用户信息
* @param user 被更新的用户
* @return 返回成功操作数据库的数据量
*/
public int updateUser(User user);
/**
* 通过用户名删除用户
* @param name 用户名
* @return 返回成功操作数据库的数据量
*/
public int deleteUser(String name);
}
package cn.zr.pringandmybatis.service;
import java.util.List;
import cn.zr.pringandmybatis.pojo.User;
public interface OperationService {
/**
* 获取所有的用户
* @return 返回用户集合
*/
List<User> getAllUserInfo();
/**
* 添加用户
* @param user 被添加的用户
* @return 返回成功操作数据库的数据量
*/
int addUser(User user);
/**
* 更新用户信息
* @param user 被更新的用户
* @return 返回成功操作数据库的数据量
*/
int updateUser(User user);
/**
* 通过用户名删除用户
* @param name 用户名
* @return 返回成功操作数据库的数据量
*/
int deleteUser(String name);
}
package cn.zr.pringandmybatis.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.zr.pringandmybatis.mapper.UserMapper;
import cn.zr.pringandmybatis.pojo.User;
import cn.zr.pringandmybatis.service.OperationService;
@Component
public class OperationServiceImpl implements OperationService {
@Resource
private UserMapper userMapper;
@Override
public List<User> getAllUserInfo() {
return userMapper.getAllUserInfo();
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
@Override
public int deleteUser(String name) {
return userMapper.deleteUser(name);
}
}
package cn.zr.pringandmybatis.utils;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.zr.pringandmybatis.pojo.User;
import cn.zr.pringandmybatis.service.OperationService;
public class TestClass {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取实现类对象
OperationService operationService = (OperationService)ac.getBean(OperationService.class);
/*//查询所有的数据
List<User> list = operationService.getAllUserInfo();
for (User user : list) {
System.out.println(user);
}*/
/*// 添加数据
User user = new User("甘", 25);
int count = operationService.addUser(user);
if (count>0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}*/
/*// 修改
User user = new User("甘雨路", 20);
int count = operationService.updateUser(user);
if (count>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}*/
// 删除
int count = operationService.deleteUser("甘露");
if (count>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}