- mybatis的映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.lky.dao.UserMapper"> <insert id="add" parameterType="com.lky.model.User"> insert into s_user(user_name,user_birthday,user_salary) values(#{name},#{birthday},#{salary}) </insert> <update id="update" parameterType="com.lky.model.User"> update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary} where user_id=#{id} </update> <delete id="delete" parameterType="int"> delete from s_user where user_id=#{id} </delete> <select id="findById" parameterType="int" resultType="com.lky.model.User"> select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user where user_id=#{id} </select> <select id="findAll" resultType="com.lky.model.User"> select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user </select> </mapper>
2.mybatis的配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <mappers> <mapper resource="com/lky/dao/UserMapper.xml"/> </mappers> </configuration>
3.Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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"> <!-- 配置数据源 DriverManagerDataSource--> <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://10.21.25.228:3306/mybatis</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <!-- mybatis的sqlSession的工厂 sqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /> <property name="typeAliasesPackage" value="com.lky.model"/> </bean> <!-- mybatis自动扫描加载sql映射文件、接口:MapperScannerConfigurer basePackage:指定sql映射文件,接口所在的包(自动扫描) sqlSessionFactory: 引用上面定义的sqlSessionFactory --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lky.dao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- 事务管理 DataSourceTransactionManager dataSource:引用上面定义的数据源 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="jdbcDataSource"/> </bean> <!-- 使用声明的事务 使用上面定义的事务 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
4.实体类
package com.lky.model; import java.util.Date; public class User { private int id; private String name; private Date birthday; private double salary; 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; } 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(); } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", salary=" + salary + "]"; } }
5.接口类
package com.lky.dao; import java.util.List; import com.lky.model.User; public interface UserMapper { public void add(User user); public void update(User user); public void delete(User user); public void findById(int id); public List<User> findAll(); }
6.测试文件
package com.lky.test; import java.util.Date; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.lky.dao.UserMapper; import com.lky.model.User; @RunWith(SpringJUnit4ClassRunner.class)//使用Spring的测试框架 @ContextConfiguration("/beans.xml")//加载spring的配置文件bean public class smTest { @Autowired //自动注入该对象 private UserMapper userMapper; @Test public void testAdd(){ User user=new User(-1,"tom",new Date(),12345); userMapper.add(user); } @Test public void testfindByName(){ System.out.println(userMapper.findByName("tom")); } @Test public void testfingAll(){ List<User> ss=userMapper.findAll(); System.out.println(ss); } }
时间: 2024-10-12 14:50:33