mybatis学习笔记二(接口注解)

  直接上代码,全部在代码里讲解。

  1.实体类

package com.home.entity;

/**
 * 此类是:user实体类
 * @author hpc
 * @2017年1月10日下午9:36:59
 */
public class User {
    private Integer user_id;
    private String user_name;
    private String user_pwd;
    
    public User(Integer user_id, String user_name, String user_pwd) {
        super();
        this.user_id = user_id;
        this.user_name = user_name;
        this.user_pwd = user_pwd;
    }
    public Integer getUser_id() {
        return user_id;
    }
    public void setUser_id(Integer user_id) {
        this.user_id = user_id;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getUser_pwd() {
        return user_pwd;
    }
    public void setUser_pwd(String user_pwd) {
        this.user_pwd = user_pwd;
    }
    @Override
    public String toString() {
        return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_pwd=" + user_pwd + "]";
    }
    
}

  2. 接口注解

  

package com.home.entityInterface;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.home.entity.User;

/**
* 此类是:映射关系接口
* @author hpc
* @2017年1月11日下午7:09:20
*/
public interface UserMapperInterface{
/**
* 获取指定id的User
* 当我们调用这个方法的时候就会去执行这一条sql语句,
* 这里面要注意点的是:
* 传进来的参数是以对象的形式传进来的,
* 所以取的时候要以对象属性来取,否则将会报错.
*/
@Select("select * from users where user_id=#{user_id}")
public User loadUser(User user);

/**
* 更新user
*/
@Update("update users set user_name=#{user_name},user_pwd=#{user_pwd} where user_id=#{user_id}")
public int updateUser(User user);
/**
* 删除user
*/
@Delete("delete from users where user_id=#{user_id}")
public int deleteUser(User user);
/**
* 插入user
*/
@Insert("insert into users(user_name,user_pwd) values(#{user_name},#{user_pwd}) ")
public int insertUser(User user);
}

 

  3.mybatis的配置文件(要和xml配置一样,都要在mybatis配置文件中注册)

 

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>
  <properties resource="jdbc.properties"/>
  
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${jdbc.driverClass}"/>
        
        <property name="url" value="${jdbc.url}"/>
        
        <property name="username" value="${jdbc.user}"/>
        
        <property name="password" value="${jdbc.password}"/>
        
      dataSource>
    environment>
  environments>
  
  <mappers>
      
       
      
      <mapper class="com.home.entityInterface.UserMapperInterface"/>
  mappers>
configuration>

   4.测试

  

package com.home.mybatis;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;

import com.home.entity.User;
import com.home.entityInterface.UserMapperInterface;
import com.home.mybatis.utils.SessionUtils;

public class TestApp {
    public static void main(String[] args) throws IOException {
        // 我将获取session的代码封装成了一个工具类,直接用工具类来获取session
        SqlSession session = SessionUtils.getSession("mybatis.xml");
        // 通过接口,直接使用session.getMapper(接口.class);获取到接口
        UserMapperInterface mapper = session.getMapper(UserMapperInterface.class);
        // 再调用接口的方法,去执行接口所注解的sql语句
        System.out.println(mapper.insertUser(new User(null,"mh", "123")));
        System.out.println(mapper.updateUser(new User(3, "hpc", "123")));
        System.out.println(mapper.deleteUser(new User(3, null, null)));
        System.out.println(mapper.loadUser(new User(1, null, null)));
        // 将数据属性到数据中去
        session.commit();
        // 关闭session
        session.close();
    } 
}

  5.结果

mybatis学习笔记二(接口注解)_第1张图片

mybatis学习笔记二(接口注解)_第2张图片

 

 

 

转载于:https://www.cnblogs.com/mohehpc/p/6279352.html

你可能感兴趣的:(mybatis学习笔记二(接口注解))