mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 02:53:57 +08:00
218 lines
5.0 KiB
Markdown
218 lines
5.0 KiB
Markdown
> 对象关系映射模型Hibernate。用来实现非常轻量级的对象的封装。将对象与数据库建立映射关系。实现增删查改。
|
||
> MyBatis与Hibernate非常相似。对象关系映射模型ORG。java对象与关系数据库映射的模型。
|
||
|
||
## 1 配置MyBatis
|
||
|
||
### 在pom.xml中添加MyBatis依赖
|
||
|
||
```
|
||
<dependency>
|
||
<groupId>org.mybatis.spring.boot</groupId>
|
||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||
<version>2.1.1</version>
|
||
</dependency>
|
||
|
||
<dependency>
|
||
<groupId>mysql</groupId>
|
||
<artifactId>mysql-connector-java</artifactId>
|
||
</dependency>
|
||
```
|
||
|
||
### 在application.properties中配置mysql的链接配置
|
||
|
||
```
|
||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||
spring.datasource.username=root
|
||
spring.datasource.password=
|
||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||
```
|
||
|
||
### 创建关系数据表
|
||
|
||
```
|
||
CREATE TABLE `User` (
|
||
`id` bigint NOT NULL AUTO_INCREMENT,
|
||
`name` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
|
||
`age` int DEFAULT NULL,
|
||
PRIMARY KEY (`id`)
|
||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
|
||
```
|
||
|
||
### 创建数据表的java对象
|
||
|
||
```
|
||
@Data
|
||
@NoArgsConstructor
|
||
public class User {
|
||
|
||
private Long id;
|
||
|
||
private String name;
|
||
private Integer age;
|
||
|
||
public User(String name, Integer age) {
|
||
this.name = name;
|
||
this.age = age;
|
||
}
|
||
}
|
||
```
|
||
|
||
### 创建数据表的操作接口
|
||
|
||
```
|
||
@Mapper
|
||
public interface UserMapper {
|
||
|
||
@Select("SELECT * FROM USER WHERE NAME = #{name}")
|
||
User findByName(@Param("name") String name);
|
||
|
||
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
|
||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||
|
||
}
|
||
```
|
||
|
||
## 2 MyBatis参数传递
|
||
|
||
### 使用@Param参数传递
|
||
|
||
```
|
||
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
|
||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||
```
|
||
|
||
### 使用map 传递参数
|
||
|
||
```
|
||
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
|
||
int insertByMap(Map<String, Object> map);
|
||
//调用
|
||
Map<String, Object> map = new HashMap<>();
|
||
map.put("name", "CCC");
|
||
map.put("age", 40);
|
||
userMapper.insertByMap(map);
|
||
```
|
||
|
||
### 使用普通java对象
|
||
```
|
||
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
|
||
int insertByUser(User user);
|
||
```
|
||
## 3 增删查改操作
|
||
|
||
```
|
||
public interface UserMapper {
|
||
|
||
@Select("SELECT * FROM user WHERE name = #{name}")
|
||
User findByName(@Param("name") String name);
|
||
|
||
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
|
||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||
|
||
@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
|
||
void update(User user);
|
||
|
||
@Delete("DELETE FROM user WHERE id =#{id}")
|
||
void delete(Long id);
|
||
}
|
||
```
|
||
对增删查改的调用
|
||
```
|
||
@Transactional
|
||
@RunWith(SpringRunner.class)
|
||
@SpringBootTest
|
||
public class ApplicationTests {
|
||
|
||
@Autowired
|
||
private UserMapper userMapper;
|
||
|
||
@Test
|
||
@Rollback
|
||
public void testUserMapper() throws Exception {
|
||
// insert一条数据,并select出来验证
|
||
userMapper.insert("AAA", 20);
|
||
User u = userMapper.findByName("AAA");
|
||
Assert.assertEquals(20, u.getAge().intValue());
|
||
// update一条数据,并select出来验证
|
||
u.setAge(30);
|
||
userMapper.update(u);
|
||
u = userMapper.findByName("AAA");
|
||
Assert.assertEquals(30, u.getAge().intValue());
|
||
// 删除这条数据,并select验证
|
||
userMapper.delete(u.getId());
|
||
u = userMapper.findByName("AAA");
|
||
Assert.assertEquals(null, u);
|
||
}
|
||
}
|
||
```
|
||
|
||
## 4 使用MyBatis的XML方式
|
||
|
||
### 在应用主类中增加mapper的扫描包配置:
|
||
```
|
||
@MapperScan("com.didispace.chapter36.mapper")
|
||
@SpringBootApplication
|
||
public class Chapter36Application {
|
||
|
||
public static void main(String[] args) {
|
||
SpringApplication.run(Chapter36Application.class, args);
|
||
}
|
||
|
||
}
|
||
```
|
||
|
||
### Mapper包下创建User表
|
||
```
|
||
public interface UserMapper {
|
||
|
||
User findByName(@Param("name") String name);
|
||
|
||
int insert(@Param("name") String name, @Param("age") Integer age);
|
||
|
||
}
|
||
```
|
||
|
||
### 在配置文件中通过mybatis.mapper-locations参数指定xml配置的位置
|
||
|
||
```
|
||
mybatis.mapper-locations=classpath:mapper/*.xml
|
||
```
|
||
### xml配置目录下创建User表的mapper配置
|
||
|
||
```
|
||
<?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="com.didispace.chapter36.mapper.UserMapper">
|
||
<select id="findByName" resultType="com.didispace.chapter36.entity.User">
|
||
SELECT * FROM USER WHERE NAME = #{name}
|
||
</select>
|
||
|
||
<insert id="insert">
|
||
INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})
|
||
</insert>
|
||
</mapper>
|
||
```
|
||
|
||
### 对xml方式进行调用
|
||
```
|
||
@Slf4j
|
||
@RunWith(SpringRunner.class)
|
||
@SpringBootTest
|
||
@Transactional
|
||
public class Chapter36ApplicationTests {
|
||
|
||
@Autowired
|
||
private UserMapper userMapper;
|
||
|
||
@Test
|
||
@Rollback
|
||
public void test() throws Exception {
|
||
userMapper.insert("AAA", 20);
|
||
User u = userMapper.findByName("AAA");
|
||
Assert.assertEquals(20, u.getAge().intValue());
|
||
}
|
||
|
||
}
|
||
``` |