Mybatis学习笔记-Mybatis与Spring的整合
项目结构
User.java实体类
public class User implements Serializable{ private static final long serialVersionUID = 1L; private int id; private String username; private int age; private String sex; //... }
UserMapper接口
public interface UserMapper { public void save(User user); public void update(User user); public void delete(User user); public User findById(int id); public List<User> findAll(); }
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"> <mapper namespace="com.mybatis.spring.mapper.UserMapper"> <cache/> <!-- namespace 必须与对应的接口全类名一致 id 必须与对应接口的某个对应的方法名一致 --> <insert id="save" parameterType="com.mybatis.spring.beans.User"> insert into users(username, age, sex)values(#{username}, #{age}, #{sex}) </insert> <update id="update" parameterType="com.mybatis.spring.beans.User"> update users set username = #{username}, age = #{age}, sex = #{sex} where id = #{id} </update> <delete id="delete" parameterType="int"> delete from users where id = #{id} </delete> <select id="findById" parameterType="int" resultType="com.mybatis.spring.beans.User"> select * from users where id = #{id} </select> <select id="findAll" resultType="com.mybatis.spring.beans.User"> select * from users </select> </mapper>
beans.xml
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 1.数据源 DriverManagerDataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> </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.mybatis.spring.beans"/> </bean> <!-- 3.mybatis自动扫描加载SQL映射文件/接口:MapperScannerConfigurer basePackage 指定SQL映射文件/接口所在的包(自动扫描) sqlSessionFactory 引用上面定义的sqlSessionFactory --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis.spring.mapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- 4.事务管理:DataSourceTransactionManager dataSource 引用数据源 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5.使用声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
测试
@RunWith(SpringJUnit4ClassRunner.class)//使用Spring的测试框架 @ContextConfiguration("/beans.xml") //加载Spring的配置文件 public class Test01 { @Autowired private UserMapper userMapper; @Test public void testAdd() { User user = new User(); user.setAge(23); user.setSex("男"); user.setUsername("Tom"); userMapper.save(user); } @Test public void testUpdate() { User user = new User(); user.setAge(23); user.setSex("男"); user.setUsername("Tom"); user.setId(2); userMapper.update(user); } @Test public void testDelete() { User user = new User(); user.setId(2); userMapper.delete(user); } @Test public void testFindById() { User user = userMapper.findById(5); System.err.println(user); } @Test public void testFindAll() { List<User>users = userMapper.findAll(); System.err.println(users); } }
http://yunpan.cn/cgFmiR3yxDydb 提取码 bb82
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1560183
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。