Maven中快速使用Mybatis(注解方式)

用尽量详细的代码做Demo

1.配置pom.xml



    4.0.0

    com.aaa
    mybatis_01
    1.0-SNAPSHOT
    
    jar

    
        
            org.mybatis
            mybatis
            3.4.5
        
        
            junit
            junit
            4.10
            test
        
        
            mysql
            mysql-connector-java
            5.1.6
            runtime
        
        
            log4j
            log4j
            1.2.12
        
    

2.配置mybatis-config.xml




    
        
    
    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
    


3.创建数据库

-- 创建表
create table user(
	id int(11) primary key auto_increment,
	username varchar(32) comment '用户名称',
	sex char(1) comment '性别',
	birthday datetime comment '生日',
	address varchar(32) comment '地址'
)

4.生成实体类

package com.aaa.entity;

import java.util.Date;

public class User {

  private long id;
  private String username;
  private String sex;
  private Date birthday;
  private String address;

  public long getId() {
    return id;
  }

  public void setId(long 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 + '\'' +
            '}';
	 }
}

5.编写持久层接口 UserDao

package com.aaa.dao;

import com.aaa.entity.User;

import java.util.List;

public interface UserDao {
    /**
     * 查询所有用户
     * @return
     */
    List findAll();

    /**
     * 添加操作
     * @param user
     * @return
     */
    int insert(User user);

    /**
     * 修改操作
     * @param user
     * @return
     */
    int update(User user);

    /**
     * 删除操作
     * @param user
     * @return
     */
    int delete(User user);

    /**
     * 查询一条数据
     * @param user
     * @return
     */
    User findOne(User user);

    /**
     * 根据名称模糊查询
     * @return
     */
    List findByName(User user);

    /**
     * 查询总条数
     * @return
     */
    int count();
}

6.在持久层接口中添加注解

package com.aaa.dao;

import com.aaa.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {
    /**
     * 查询所有
     * @return
     */
    @Select({"select * from user"})
    List findAll();

    /**
     * 添加操作
     * @param user
     * @return
     */
    @Insert("insert into user(username,sex,address,birthday) values(#{username},#{sex},#{address},#{birthday})")
    int insert(User user);

    /**
     * 修改操作
     * @param user
     * @return
     */
    @Update("update user set username=#{username},sex=#{sex},address=#{address},birthday=#{birthday} where id=#{id}")
    int update(User user);

    /**
     * 删除操作
     * @param user
     * @return
     */
    @Delete("delete from user where id=#{id}")
    int delete(User user);

    /**
     * 查询一条数据
     * @param user
     * @return
     */
    @Select("select * from user where id=#{id}")
    User findOne(User user);

    /**
     * 根据名称模糊查询
     * @return
     */
    @Select("select * from user where username like #{username}")
    List findByName(User user);

    /**
     * 查询总条数
     * @return
     */
    @Select("select count(*) from user")
    int count();
}

7.编写测试类

import com.aaa.dao.UserDao;
import com.aaa.entity.User;
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.After;
import org.junit.Before;
import org.junit.Test;

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

public class doMain {
    InputStream in = null;
    SqlSessionFactory factory = null;
    SqlSession session = null;
    UserDao userDao =null;

    @Before
    public void init() throws Exception{
        //1.读取配置文件
        in = Resources.getResourceAsStream("mybatis-config.xml");
        //3.使用构建者创建工厂对象 SqlSessionFactory
        factory = new SqlSessionFactoryBuilder().build(in);
        //4.使用 SqlSessionFactory 生产 SqlSession 对象
        session = factory.openSession();
        //5.使用 SqlSession 创建 dao 接口的代理对象
        userDao = session.getMapper(UserDao.class);
    }

    @After
    public void end() throws IOException {
        //6.提交
        session.commit();
        //7.释放资源
        session.close();
        in.close();
    }

    @Test
    public void findAll(){

        //6.使用代理对象执行查询所有方法
        List users = userDao.findAll();
        for(User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void insert(){
        User user = new User();
        user.setUsername("test mybatis");
        user.setSex("男");
        user.setAddress("河南郑州");
        user.setBirthday(new Date());
        System.out.println(userDao.insert(user));
    }

    @Test
    public void update(){
        User user = new User();
        user.setUsername("update mybatis");
        user.setSex("男");
        user.setAddress("中国上海");
        user.setBirthday(new Date());
        user.setId(5);
        System.out.println(userDao.update(user));
    }

    @Test
    public void delete(){
        User user = new User();
        user.setId(24);
        System.out.println(userDao.delete(user));
    }

    @Test
    public void findOne(){
        User user = new User();
        user.setId(1);
        System.out.println(userDao.findOne(user));
    }

    @Test
    public void findByName(){
        User user = new User();
        user.setUsername("%a%");
        List users = userDao.findByName(user);
        for (User user1 : users){
            System.out.println(user1);
        }
    }

    @Test
    public void count(){
        System.out.println(userDao.count());
    }
}

8.项目结构

Maven中快速使用Mybatis(注解方式)_第1张图片

你可能感兴趣的:(MyBatis,java,maven,mybatis)