ORM全称是:Object Relational Mapping(对象关系映射),其主要作用是在编程中,把面向对象的概念跟数据库中表的概念对应起来。举例来说就是,我定义一个对象,那就对应着一张表,这个对象的实例,就对应着表中的一条记录。
Spring中对哪些框架提供支持?
hibernate:全自动
mybatis:半自动 工具有:tk-mybatis、mybatis plus
JPA
JPA规范
- 在数据库中
一个类对应一张表
一个对象对应一行记录
一个属性对应一个字段
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
-
数据表
-
orm模块结构
- pom.xml
4.0.0
com.spring
ORM
1.0-SNAPSHOT
ORM
http://www.example.com
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
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${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.springframework
spring-aop
${spring.version}
org.aspectj
aspectjrt
${aspectj.version}
org.aspectj
aspectjweaver
${aspectj.version}
junit
junit
${junit.version}
test
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
provided
cn.hutool
hutool-all
${hutool.version}
com.google.zxing
core
3.3.3
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
maven-site-plugin
3.7.1
maven-project-info-reports-plugin
3.0.0
- jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=Root
log4j.properties
##日志输出的级别,以及配置记录方案
log4j.rootLogger=debug, stdout,file
##设置日志记录到控制台的方式
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
##设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=logs/my_log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
- spring_mybatis.xml
mappers = com.spring.orm.dao.BaseDAO
IDENTITY = MYSQL
- User类
package com.spring.orm.entity;
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@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;
}
- UserDao接口
package com.spring.orm.dao;
import com.spring.orm.entity.User;
/*UserDAO,继承通用DAO接口基础的CRUD功能*/
public interface UserDAO extends BaseDAO {
}
- BaseDao接口
package com.spring.orm.dao;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/*通用DAO接口*/
public interface BaseDAO extends Mapper, MySqlMapper {
}
- UserService接口
package com.spring.orm.service;
import com.spring.orm.entity.User;
import java.util.List;
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);
}
- UserServiceImpl实现类
package com.spring.orm.service.Impl;
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;
//标注本类是一个Service组件
@Service
//在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);
}
}
- 对UserServiceImpl单元测试
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.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring_mybatis.xml"})
public class UserServiceImplTest {
@Autowired
private UserService userService;
@Test
public void insertUser() {
User user = new User();
user.setAccount("test");
user.setPassword("8888888");
user.setCredits(999);
int n = userService.insertUser(user);
assertEquals(1, n);
}
@Test
public void deleteUser() {
int n = userService.deleteUser(1);
assertEquals(1,n);
}
@Test
public void updateUser() {
User user = userService.getUser(10);
user.setPassword("00000000");
user.setCredits(1111111);
int n = userService.updateUser(user);
assertEquals(1, n);
}
@Test
public void selectUsers() {
List users = userService.selectUsers();
users.forEach(user -> System.out.println(user));
}
@Test
public void getUser() {
User user = userService.getUser(1);
System.out.println(user);
}
}