MyBatis DAO Layer Development-Mapper dynamic proxy method, mybatismapper

Source: Internet
Author: User

MyBatis DAO Layer Development-Mapper dynamic proxy method, mybatismapper

SqlMapConfig. 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>
     <environments default = "development">
         <environment id = "development">
             <!-Use jdbc transaction management->
             <transactionManager type = "JDBC" />
             <!-Database connection pool->
             <dataSource type = "POOLED">
                 <property name = "driver" value = "com.mysql.jdbc.Driver" />
                 <property name = "url" value = "jdbc: mysql: // localhost: 3306 / mybatis? characterEncoding = utf-8" />
                 <property name = "username" value = "root" />
                 <property name = "password" value = "011220" />
             </ dataSource>
         </ environment>
     </ environments>
     <mappers>
         <mapper resource = "sqlmap / user.xml" />
         <mapper resource = "mapper / mapper.xml" />
     </ mappers>
</ configuration>

Mapper. 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 = "cn.nwtxxb.mybatis.mapper.UserMapper">
    <!-Query user information based on user id->
    <select id = "getUserById" parameterType = "int" resultType = "cn.nwtxxb.mybatis.po.User">
        select * from user where id = # (id)
    </ select>
    <!-Query user information based on user name->
    <select id = "getUserByName" parameterType = "java.lang.String" resultType = "cn.nwtxxb.mybatis.po.User">
        select * from user where username like '% $ (value}%'
    </ select>
    <!-Add user->
    <insert id = "insertUser" parameterType = "cn.nwtxxb.mybatis.po.User">
        <!-selectKey returns the primary key->
        <selectKey keyProperty = "id" order = "AFTER" resultType = "java.lang.Integer">
            select LAST_INSERT_ID ()
        </ selectKey>
        insert into user (username, birthday, sex, address) values (# {username}, # {birthday}, # {sex}, # {address})
    </ insert>
    <!-Delete user->
    <delete id = "deleteUserById" parameterType = "int">
        delete from user where id = # {id}
    </ delete>
    <!-Modify user->
    <update id = "updateUser" parameterType = "int">
        update user set username = # {username}, birthday = # {birthday}, sex = # {sex}, address = # {address} where id = # {id}
    </ update>
</ mapper>

UserMapper. java

 
package cn.nwtxxb.mybatis.mapper;

import java.util.List;

import cn.nwtxxb.mybatis.po.User;

public interface UserMapper {
    User getUserById(int id);
    List<User> getUserByName(String name);
    void insertUser(User user);
}

UserMapperTest. java

package cn.nwtxxb.mybatis.mapper.test;

import java.io.InputStream;
import java.util.Date;
Import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.nwtxxb.mybatis.mapper.UserMapper;
import cn.nwtxxb.mybatis.po.User;

public class UserMapperTest {
    private SqlSessionFactory sqlSessionFactory = null;
    @Before
    public void init () throws Exception {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ();
        InputStream inputStream = Resources.getResourceAsStream ("SqlMapConfig.xml");
        sqlSessionFactory = sqlSessionFactoryBuilder.build (inputStream);
    }
    @Test
    public void testGetUserById () {
        SqlSession sqlSession = sqlSessionFactory.openSession ();
        UserMapper userMapper = sqlSession.getMapper (UserMapper.class);
        User user = userMapper.getUserById (10);
        sqlSession.close ();
        System.out.println (user);
    }

    @Test
    public void testGetUserByName () {
        SqlSession sqlSession = sqlSessionFactory.openSession ();
        UserMapper userMapper = sqlSession.getMapper (UserMapper.class);
        List <User> list = userMapper.getUserByName ("Zhang");
        sqlSession.close ();
        for (User user: list) {
            System.out.println (user);
        }
    }

    @Test
    public void testInsertUser () {
        SqlSession sqlSession = sqlSessionFactory.openSession ();
        UserMapper userMapper = sqlSession.getMapper (UserMapper.class);
        User user = new User ();
        user.setUsername ("Wang Ming");
        user.setSex ("2");
        user.setBirthday (new Date ());
        user.setAddress ("Arctic Circle");
        userMapper.insertUser (user);
        sqlSession.commit ();
        sqlSession.close ();
    }

}

User. java

 
package cn.nwtxxb.mybatis.po;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private String sex;
    private Date birthday;
    private String address;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
                + address + "]";
    }

}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/a772304419/article/details/79069308


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.