首页 > 代码库 > Mybatis学习笔记-Mybatis与Spring的整合
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="http://www.mamicode.com/jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="http://www.mamicode.com/root"/> <property name="password" value="http://www.mamicode.com/123456"/> <property name="driverClassName" value="http://www.mamicode.com/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="http://www.mamicode.com/com.mybatis.spring.beans"/> </bean> <!-- 3.mybatis自动扫描加载SQL映射文件/接口:MapperScannerConfigurer basePackage 指定SQL映射文件/接口所在的包(自动扫描) sqlSessionFactory 引用上面定义的sqlSessionFactory --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="http://www.mamicode.com/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
Mybatis学习笔记-Mybatis与Spring的整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。