Chinaunix首页 | 论坛 | 博客
  • 博客访问: 515745
  • 博文数量: 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:26:22

 一、 数据库建表      

  1. CREATE TABLE `user` (
  2.   `userId` int(11) NOT NULL AUTO_INCREMENT,
  3.   `UserName` varchar(45) NOT NULL,
  4.   `UserEmail` varchar(200) NOT NULL,
  5.   PRIMARY KEY (`userId`),
  6.   UNIQUE KEY `userId_UNIQUE` (`userId`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
二、maven配置

  1. <dependencies>
  2.         <dependency>
  3.             <groupId>org.mybatis</groupId>
  4.             <artifactId>mybatis</artifactId>
  5.             <version>3.2.6</version>
  6.         </dependency>

  7.         <dependency>
  8.             <groupId>junit</groupId>
  9.             <artifactId>junit</artifactId>
  10.             <version>4.12</version>
  11.             <scope>test</scope>
  12.         </dependency>
  13.         
  14.         <dependency>
  15.             <groupId>c3p0</groupId>
  16.             <artifactId>c3p0</artifactId>
  17.             <version>0.9.1.2</version>
  18.         </dependency>
  19.         
  20.         <dependency>
  21.             <groupId>mysql</groupId>
  22.             <artifactId>mysql-connector-java</artifactId>
  23.             <version>6.0.2</version>
  24.         </dependency>
  25.     </dependencies>

三、mybasit 配置

  • jdbc.properties 配置 数据连接
    1. driver=com.mysql.cj.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=true&serverTimezone=UTC
    3. username=mybatis
    4. password=mybatis
    5. maxActive= 50
  • 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.     </mappers>
    21. </configuration>

四、建立User的POJO   


  1. package com.elements.user.model;

  2. public class User {

  3.     public int getUserId() {
  4.         return userId;
  5.     }
  6.     public void setUserId(int userId) {
  7.         this.userId = userId;
  8.     }
  9.     public String getUserName() {
  10.         return UserName;
  11.     }
  12.     public void setUserName(String userName) {
  13.         UserName = userName;
  14.     }
  15.     public String getUserEmail() {
  16.         return UserEmail;
  17.     }
  18.     public void setUserEmail(String userEmail) {
  19.         UserEmail = userEmail;
  20.     }
  21.     private int userId;
  22.     private String UserName;
  23.     private String UserEmail;
  24.     
  25.     
  26.     @Override
  27.     public String toString() {
  28.         return "User [userId=" + userId + ", UserName=" + UserName
  29.                 + ", UserEmail=" + UserEmail + "]";
  30.     }
  31.     
  32.     
  33.     

  34. }
五、UserMapper配置文件 配置sql        

  • UserMapper  配置文件       

  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">

  6.   <resultMap type="com.elements.user.model.User" id="usermap">
  7.         <id column="userId" property="userId"/>
  8.         <result column="UserName" property="UserName"/>
  9.         <result column="UserEmail" property="UserEmail"/>
  10.     </resultMap>
  11.   
  12.  
  13.     <select id="getAllUser" resultMap="usermap">
  14.         select * from mybatis.user
  15.     </select>
  16.     
  17.     <select id="getUserById" resultMap="usermap" parameterType="String">
  18.         select * from mybatis.user where userId= #{userId}
  19.     </select>

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

  36. </mapper>
  • 将配置文件加入mybatis-config.xml的  mappers中
    1. <mappers>

    2.     <mapper resource="com/elements/user/mapper/UserMapper.xml" />

    3.     </mappers>
六、建立测试类

  1. public class TestUser {

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

  9.         SqlSession session = sqlSessionFactory.openSession();
  10.         try {

  11.             List<User> userlist = session
  12.                     .selectList("com.elements.user.mapper.getAllUser");

  13.             System.out.print(userlist);
  14.  
  15.         }catch(Exception e){
  16.             System.out.print(e);
  17.         }
  18.         finally {
  19.             session.close();
  20.         }

  21.     }

  22.     @Test
  23.     public void TestSelect() throws IOException {
  24.         //
  25.         String resource = "mybatis-config.xml";
  26.         InputStream inputStream = Resources.getResourceAsStream(resource);
  27.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
  28.                 .build(inputStream);

  29.         SqlSession session = sqlSessionFactory.openSession();
  30.         try {
  31.             User user = (User) session.selectOne(
  32.                     "com.elements.user.mapper.getUserById", "1");

  33.             System.out.print(user);

  34.         } finally {
  35.             session.close();
  36.         }

  37.     }

  38.     @Test
  39.     public void TestInsert() throws IOException {

  40.         String resource = "mybatis-config.xml";
  41.         InputStream inputStream = Resources.getResourceAsStream(resource);
  42.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
  43.                 .build(inputStream);

  44.         SqlSession session = sqlSessionFactory.openSession();
  45.         try {
  46.             User user = new User();
  47.             user.setUserName("abc");
  48.             user.setUserEmail("aaaaa");

  49.             session.update("com.elements.user.mapper.insert", user);

  50.             session.commit();

  51.         } finally {
  52.             session.close();
  53.         }

  54.     }

  55.     @Test
  56.     public void TestUpdate() throws IOException {

  57.         String resource = "mybatis-config.xml";
  58.         InputStream inputStream = Resources.getResourceAsStream(resource);
  59.         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
  60.                 .build(inputStream);

  61.         SqlSession session = sqlSessionFactory.openSession();
  62.         try {
  63.             User user = new User();
  64.             user.setUserId(7);
  65.             user.setUserName("aaaaa");
  66.             user.setUserEmail("aaaaa");

  67.             session.update("com.elements.user.mapper.update", user);

  68.             session.commit();

  69.         } finally {
  70.             session.close();
  71.         }

  72.     }

  73. }

项目代码下载:
      

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