jar文件下载地址:http://download.csdn.net/detail/huhui_bj/8575533
1、准备jar包
【mybatis】
mybatis⑶.2.0.jar
mybatis-spring⑴.1.1.jar
log4j⑴.2.17.jar
【spring】
spring-aop⑶.2.0.RELEASE.jar
spring-beans⑶.2.0.RELEASE.jar
spring-context⑶.2.0.RELEASE.jar
spring-core⑶.2.0.RELEASE.jar
spring-expression⑶.2.0.RELEASE.jar
spring-jdbc⑶.2.0.RELEASE.jar
spring-test⑶.2.4.RELEASE.jar
spring-tx⑶.2.0.RELEASE.jar
aopalliance⑴.0.jar
cglib-nodep⑵.2.3.jar
commons-logging⑴.1.1.jar
【MYSQL驱动包】
mysql-connector-java⑸.0.4-bin.jar
2、创建数据表和实体类
CREATE TABLE s_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
user_birthday DATE,
user_salary DOUBLE
)
public class User {
private int id;
private String name;
private Date birthday;
private double salary;
//set,get方法
}
3、DAO接口
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
User findById(int id);
List<User> findAll();
}
4、SQL映照文件:userMapper.xml(与接口疏忽大小写同名)
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis⑶-mapper.dtd">
<!--
UserMapper.java的文件名必须和userMapper.xml相同
namespace:必须与对应的接口全类名1致
id:必须与对应接口的某个方法名1致
-->
<mapper namespace="com.atguigu.mybatis.test9.UserMapper">
<resultMap type="User" id="userResult">
<result column="user_id" property="id"/>
<result column="user_name" property="name"/>
<result column="user_birthday" property="birthday"/>
<result column="user_salary" property="salary"/>
</resultMap>
<!-- 获得插入数据后的id -->
<insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">
insert into s_user(user_name,user_birthday,user_salary)
values(#{name},#{birthday},#{salary})
</insert>
<update id="update">
update s_user
set user_name = #{name},
user_birthday = #{birthday},
user_salary = #{salary}
where user_id = #{id}
</update>
<delete id="delete">
delete from s_user
where user_id = #{id}
</delete>
<select id="findById" resultMap="userResult">
select *
from s_user
where user_id = #{id}
</select>
<select id="findAll" resultMap="userResult">
select *
from s_user
</select>
</mapper>
5、Spring配置文件beans.xml
<?xml version="1.0" encoding="UTF⑻"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans⑶.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context⑶.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx⑶.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource / typeAliasesPackage
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.atuigu.spring_mybatis2.domain"/>
</bean>
<!--
3. mybatis自动扫描加载Sql映照文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atuigu.spring_mybatis2.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
6、mybatis的配置文件: mybatis-config.xml
<?xml version="1.0" encoding="UTF⑻" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis⑶-config.dtd">
<configuration>
<!-- Spring整合myBatis后,这个配置文件基本可以不要了-->
<!-- 设置外部配置文件 -->
<!-- 设置种别名 -->
<!-- 设置
数据库连接环境 -->
<!-- 映照文件 -->
</configuration>
7、测试
@RunWith(SpringJUnit4ClassRunner.class) //使用Springtest测试框架
@ContextConfiguration("/beans.xml") //加载配置
public class SMTest {
@Autowired //注入
private UserMapper userMapper;
@Test
public void save() {
User user = new User();
user.setBirthday(new Date());
user.setName("marry");
user.setSalary(300);
userMapper.save(user);
System.out.println(user.getId());
}
@Test
public void update() {
User user = userMapper.findById(2);
user.setSalary(2000);
userMapper.update(user);
}
@Test
public void delete() {
userMapper.delete(3);
}
@Test
public void findById() {
User user = userMapper.findById(1);
System.out.println(user);
}
@Test
public void findAll() {
List<User> users = userMapper.findAll();
System.out.println(users);
}
}