WeiG's blog
首页
  • 类目

    • Java
    • Mysql
  • 学习笔记

    • 《SpringBoot-学习笔记》
  • 大杂烩
  • Docker
  • Git
  • 博客搭建
  • 程序人生
  • 所思所想
  • 人生游记
  • 归档
  • 分类
  • 标签
收藏
关于我

Wei-G

即使再小的帆也可以远航!
首页
  • 类目

    • Java
    • Mysql
  • 学习笔记

    • 《SpringBoot-学习笔记》
  • 大杂烩
  • Docker
  • Git
  • 博客搭建
  • 程序人生
  • 所思所想
  • 人生游记
  • 归档
  • 分类
  • 标签
收藏
关于我
  • 01|springboot配置mybatis
    • xml配置
    • 注解配置
  • 02|springBoot配置easyExcel
  • 《SpringBoot-学习笔记》
gangzi
2022-01-19
时间 2分钟

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

创建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

测试能否链接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

测试成功后

启动类上添加@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

根据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

@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

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
#SpringBoot#mybatis
上次更新: 2022/01/19, 11:36:06
02|springBoot配置easyExcel

02|springBoot配置easyExcel→

本站总访问量 次 | 本站访客数 人
Copyright © 2021-2022 Gang zi | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 生命绿
  • 收获黄
  • 天空蓝
  • 激情红
  • 高贵紫