day06-02 Mybatis学习

1、了解Mybatis

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
本节实例采用封装的Mybatis通用Mapper和Spring整合,演示基本的单表CRUD,复杂的SQL建议自定义
tk-mybatis地址: https://github.com/abel533/Mapper

2、User练习

  • 新建db_spring数据库,并在库中新建一个表t_user


    day06-02 Mybatis学习_第1张图片
    t_user.png
  • 做好准备工作


    day06-02 Mybatis学习_第2张图片
    day06-01.png

    day06-02 Mybatis学习_第3张图片
    day06-02.png

    day06-02 Mybatis学习_第4张图片
    day06-03.png
  • 添加依赖


    UTF-8
    1.8
    1.8
    5.1.5.RELEASE
    1.9.2
    4.12
    1.2.17
    1.7.12
    4.5.1
    5.1.47
    3.5.0
    2.0.0
    4.1.5
    1.1.14
    1.18.6
  

  
    
      junit
      junit
      4.12
      test
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    
      org.aspectj
      aspectjweaver
      ${aspectj.version}
    
    
      org.aspectj
      aspectjrt
      ${aspectj.version}
    
    
    
      org.springframework
      spring-test
      ${spring.version}
    
    
    
      junit
      junit
      ${junit.version}
      test
    
    
    
      log4j
      log4j
      ${log4j.version}
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
      cn.hutool
      hutool-all
      ${hutool.version}
    
    
      com.google.zxing
      core
      3.3.3
    
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
      mysql
      mysql-connector-java
      ${mysql.version}
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
      org.mybatis
      mybatis-spring
      ${mybatis-spring.version}
    
    
      tk.mybatis
      mapper
      ${tk-mybatis.version}
    
      
          com.alibaba
          druid
          ${druid.version}
      
    
      org.projectlombok
      lombok
      ${lombok.version}
      provided
    
  
  • 配置文件


    
    

    
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
    

    
    
        
        
        
        
            
                mappers = com.spring.orm.dao.BaseDAO
                IDENTITY = MYSQL
            
        
    

    
    
        
    
    
    

  • User类
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by Administrator on 2019/3/11.
 */
@Table(name="t_user")
@Data
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String account;
    private String password;
    private Integer credits;
}
  • DAO类
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface BaseDAO extends Mapper,MySqlMapper{
}
import com.spring.orm.entity.User;

/**
 * Created by Administrator on 2019/3/11.
 * UserDAO,继承通用DAO接口基础的CRUD功能
 */
public interface UserDAO extends BaseDAO {
}
  • Service类
import java.util.List;
/**
 * Created by Administrator on 2019/3/11.
 */
public interface UserService {
    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    int insertUser(User user);
    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    int deleteUser(long id);
    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    int updateUser(User user);
    /**
     * 查询所有用户
     *
     * @return
     */
    List selectUsers();
    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    User getUser(long id);
}
  • ServiceImpl类
import com.spring.orm.dao.UserDAO;
import com.spring.orm.entity.User;
import com.spring.orm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * Created by Administrator on 2019/3/11.
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    @Override
    public int insertUser(User user) {
        return userDAO.insert(user);
    }
    @Override
    public int deleteUser(long id) {
        return userDAO.deleteByPrimaryKey(id);
    }
    @Override
    public int updateUser(User user) {
        return userDAO.updateByPrimaryKey(user);
    }
    @Override
    public List selectUsers() {
        return userDAO.selectAll();
    }
    @Override
    public User getUser(long id) {
        return userDAO.selectByPrimaryKey(id);
    }
}
  • Test类
import com.spring.orm.entity.User;
import com.spring.orm.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import static org.junit.Assert.*;
/**
 * Created by Administrator on 2019/3/11.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring_mybatis.xml"})
public class UserServiceImplTest {
    @Autowired
    private UserService userService;
    @Test
    public void insertUser() throws Exception {
        User user=new User();
        user.setAccount("test");
        user.setPassword("88888888");
        user.setCredits(999);
        int n=userService.insertUser(user);
        assertEquals(1,n);
    }
    @Test
    public void deleteUser() throws Exception {
        int n=userService.deleteUser(2);
        assertEquals(1,n);
    }
    @Test
    public void updateUser() throws Exception {
        User user=userService.getUser(3);
        user.setPassword("000000");
        user.setCredits(11111);
        int n=userService.updateUser(user);
        assertEquals(1,n);
    }
    @Test
    public void selectUsers() throws Exception {
        List user=userService.selectUsers();
        user.forEach(user1 -> System.out.println(user));
    }
    @Test
    public void getUser() throws Exception {
        User user=userService.getUser(1);
        System.out.println(user);
    }
}

你可能感兴趣的:(day06-02 Mybatis学习)