com.frank.domain

  User.java

 1 package com.frank.domain;
 2 
 3 public class User {
 4     private Integer id;
 5     private String name;
 6     private Integer age;
 7     public Integer getId() {
 8         return id;
 9     }
10     public void setId(Integer id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public Integer getAge() {
20         return age;
21     }
22     public void setAge(Integer age) {
23         this.age = age;
24     }
25     public User(Integer id, String name, Integer age) {
26         super();
27         this.id = id;
28         this.name = name;
29         this.age = age;
30     }
31     public User() {
32         super();
33     }
34     @Override
35     public String toString() {
36         return "User [id=" + id + ", name=" + name + ", age="
37                 + age + "]";
38     }
39     
40 
41 }
View Code

com.frank.mapper

  UserMapper.java

 1 package com.frank.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.frank.domain.User;
 6 
 7 public interface UserMapper {
 8 
 9     public void addUser(User user);
10 
11     public void delUser(Integer id);
12     
13     public User findById(Integer id);
14     
15     public List<User> findAll();
16     
17     public void updateUser(User user);
18 
19 
20 }
View Code

  UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.frank.mapper.UserMapper">
 6     
 7     <resultMap type="User" id="userResult">
 8         <result column="user_id" property="id"/>
 9         <result column="user_name" property="name"/>
10         <result column="user_age" property="age"/>
11     </resultMap>
12     
13     <insert id="addUser" parameterType ="User">
14         insert into s_user(user_name,user_age) values(#{name},#{age})
15     </insert>
16     
17     <delete id="delUser" parameterType="int">
18         delete from s_user where user_id=#{id}
19     </delete>
20 
21     <select id="findById" parameterType="int" resultMap="userResult">
22         select * from s_user where user_id=#{id}
23     </select>
24     
25     <select id="findAll" resultMap="userResult">
26         select *  from s_user
27     </select>
28     
29     <update id="updateUser" parameterType="User">
30         update s_user set user_name=#{name},user_age=#{age} where user_id=#{id}
31     </update>
32 
33 
34 </mapper>
View Code

com.frank.test

  Test_mybatis.java

 1 package com.frank.test;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import com.frank.domain.User;
12 import com.frank.mapper.UserMapper;
13 
14 
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @ContextConfiguration("/applicationContext.xml")
17 public class Test_mybatis {
18     
19     @Autowired
20     private UserMapper userMapper;
21     @Test
22     public void testInsert(){
23         User user=new User(null, "frank", 15);
24         userMapper.addUser(user);
25         
26     }
27     @Test
28     public void testDel(){
29         userMapper.delUser(5);
30     }
31     
32     @Test
33     public void testFindById(){
34         User u = userMapper.findById(2);
35         System.out.println(u);
36     }
37     @Test
38     public void testFindAll(){
39         List<User> list=userMapper.findAll();
40         System.out.println(list);
41     }
42     
43 
44 }
View Code

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
 5     xmlns:tx="http://www.springframework.org/schema/tx">
 6 
 7     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 8         <property name="driverClassName" value="com.mysql.jdbc.Driver">
 9         </property>
10         <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis">
11         </property>
12         <property name="username" value="root"></property>
13         <property name="password" value="frank1994"></property>
14     </bean>
15     
16     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
17         <property name="dataSource" ref="dataSource"></property>    
18         <property name="typeAliasesPackage" value="com.frank.domain"></property>
19     </bean>
20     
21     <!-- mybatis自动扫描,加载映射文件 -->
22     <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
23         <property name="basePackage" value="com.frank.mapper"></property>        
24         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
25      </bean>
26      
27      <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
28         <property name="dataSource" ref="dataSource"></property> 
29      </bean>
30      
31      <tx:annotation-driven transaction-manager="txManager" />
32 
33 </beans>
View Code

mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6     
7 </configuration>
View Code