01|springboot配置mybatis
两种配置方式
- xml配置
- 注解配置
# xml配置
自查
# 注解配置
配置pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
创建application.yml
文件
server:
port: 8080
servlet:
context-path: /
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
测试能否链接mysql
@SpringBootApplication
@MapperScan("com.wg.demo.mapper")
public class EasyexcelApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM member");
System.out.println(result);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
测试成功后
启动类上添加@MapperScan("com.wg.demo.mapper")
@SpringBootApplication
@MapperScan("com.wg.demo.mapper")
public class EasyexcelApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
根据com.wg.demo.mapper
路径,创建mapper
包
创建MemberMapper
类
若启动类上,添加了@MapperScan
注解,则具体的mapper类上不用再添加@Mapper
注解
@Mapper
public interface MemberMapper {
@Select("SELECT * FROM member")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "password", column = "password"),
@Result(property = "nickname", column = "nickname"),
@Result(property = "birthday", column = "birthday"),
@Result(property = "phone", column = "phone"),
@Result(property = "gender", column = "gender"),
})
List<Member> getMember();
@Options(useGeneratedKeys = true, keyProperty = "po.id", keyColumn = "id")
@Insert("insert into member (username, password, nickname, birthday, phone, gender) values (#{po.username}, #{po.password}, #{po.nickname}, #{po.birthday}, #{po.phone}, #{po.gender})")
int save(@Param("po") MemberPo po);
@Update("update member set `username`=#{username} where id = #{id}")
int add(@Param("id") int id, @Param("username") String username);
@Delete("delete from member where id = #{id}")
int delete(@Param("id") int id);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Insert
注解用来插入
@Options
注解用来指定一些配置信息,比如上面的语句中用来配置将插入的id,保存到参数MemberPo的id字段上
在service类中,通过@Autowired
注入来使用
@Service
public class MemberServiceImpl {
@Autowired
MemberMapper mapper;
public List<Member> getMember(){
return mapper.getMember();
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
over
稍微复杂高级点的操作
/**
* foreach 查询
*
* @param ids
* @return
*/
@Select("<script> select * from member where id in " +
"<foreach collection='ids' index='index' item='id' open='(' separator=',' close=')'>" +
"#{id}" +
"</foreach></script>")
List<MemberPo> getByIds(@Param("ids") List<Integer> ids);
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
上次更新: 2022/01/19, 11:36:06