`

spring+mybatis配置

阅读更多
1.用spring配置mybatis
<?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:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.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.atguigu.day03_ms.bean"/>
</bean>

<!--
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
-->
<bean id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atguigu.day03_ms.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>

<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="manager" />
</beans>

2.创建实体类
public class User {

private int id;
private String name;
private Date birthday;
private double salary;

public User(int id, String name, Date birthday, double salary) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.salary = salary;
}

public User() {
super();
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}
3.创建UserMapper
public interface UserMapper {

void save(User user);

void update(User user);

void delete(int id);

User findById(int id);

List<User> findAll();

}
4.创建userMapper.xml
<?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">
<!--namespace必须是接口的全类名  -->
<mapper namespace="com.atguigu.day03_ms.mapper.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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics