Chinaunix首页 | 论坛 | 博客
  • 博客访问: 515697
  • 博文数量: 99
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 783
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-12 09:11
文章分类

全部博文(99)

文章存档

2023年(2)

2022年(1)

2020年(1)

2019年(1)

2018年(4)

2017年(16)

2016年(60)

2015年(1)

2013年(3)

2006年(10)

我的朋友

分类: Java

2016-04-26 10:45:19

MyBatis 入门(一)--基础方式 


项目的基础配置见入门,这里只把变化点说明

一、建立接口 UserMapper.java


  1. public interface UserMapper {

  2.     public List<User> getAllUser();

  3.     public User getUserById(String userId);

  4.     public int insert(User user);

  5.     public int update(User user);

  6.     public int delete(User user);

  7. }

二 修改 UserMapper.xml          


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3.     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.     "">
  5. <mapper namespace="com.elements.user.mapper.UserMapper"
  6. 注意这里与原来的方式不同 这里必须和你的接口路径+名称一直(不包括.java)
  7.   <resultMap type="com.elements.user.model.User" id="usermap">
  8.         <id column="userId" property="userId"/>
  9.         <result column="UserName" property="UserName"/>
  10.         <result column="UserEmail" property="UserEmail"/>
  11.     </resultMap>
  12.   
  13.  
  14.     <select id="getAllUser" resultMap="usermap">
  15.         select * from mybatis.user
  16.     </select>
  17.     
  18.     <select id="getUserById" resultMap="usermap" parameterType="String">
  19.         select * from mybatis.user where userId= #{userId}
  20.     </select>

  21.  
  22.     <insert id="insert" parameterType="com.elements.user.model.User">
  23.      insert into mybatis.user (UserName, UserEmail) values (
  24.           #{UserName}, #{UserEmail}
  25.      )
  26.     </insert>
  27.     
  28.     <update id="update" parameterType="com.elements.user.model.User">
  29.      update mybatis.user set UserName=#{UserName},
  30.      UserEmail=#{UserEmail}
  31.      where userId= #{userId}
  32.     </update>
  33.     
  34.     <delete id="delete" parameterType="String">
  35.      delete mybatis.user where userId= #{userId}
  36.     </delete>

  37. </mapper>

三、 mybatis-config.xml配置

        这里变成了接口的类  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4.   "">

  5. <configuration>
  6.     <properties resource="jdbc.properties" />
  7.     <environments default="dev">
  8.         <environment id="dev">
  9.             <transactionManager type="JDBC" />
  10.             <dataSource type="POOLED">
  11.                 <property name="driver" value="${driver}" />
  12.                 <property name="url" value="${url}" />
  13.                 <property name="username" value="${username}" />
  14.                 <property name="password" value="${password}" />
  15.             </dataSource>
  16.         </environment>
  17.     </environments>
  18.     <mappers>
  19.     
  20.         <mapper class="com.elements.user.mapper.UserMapper"/>
  21.     </mappers>
  22. </configuration>

四、测试类            

  1. public class TestUser {

  2.     @Test
  3.     public void TestSelectAll() throws IOException {
  4.         String resource = "mybatis-config.xml";
  5.         InputStream inputStream = Resources.getResourceAsStream(resource);
  6.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
  7.                 .build(inputStream);
  8.         
  9.         SqlSession session = sqlSessionFactory.openSession();

  10.         try {

  11.             UserMapper user = (UserMapper) session.getMapper(UserMapper.class);
  12.             List<User> userlist = user.getAllUser();
  13.             System.out.print(userlist);

  14.         } finally {
  15.             session.close();
  16.         }

  17.     }
  18. }

项目代码下载:



   

阅读(3749) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~