首页 > 编程语言 > 详细

TZ_11_Spring-Boot的整合SpringMvc和MyBatis

时间:2019-09-02 23:12:35      阅读:80      评论:0      收藏:0      [点我收藏+]

1.整合SpringMVC

  虽然默认配置已经可以使用SpringMVC了,不过我们有时候需要进行自定义配置。

 1>修改方式 通过application.yaml 此名字不需要使用@PropertySource("classpath:jdbc.properties")

server:
  #配置端口号
  port: 8080
  #配置前端控制器
  servlet:
    path: "*.html"
#配置jdbc
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop
    username: root
    password: 12345
#配置别名
mybatis:
  type-aliases-package: com.hdh.pojo

 

2.jar工程访问静态资源(基本用不上)

在ResourceProperties类中明确表明了默认加载静态资源的文件夹  只要静态资源放在这些目录中任何一个,SpringMVC都会帮我们处理。

 

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"};

 

 

 

 

 3.整合mybatis

  1>SpringBoot官方并没有提供Mybatis的启动器,不过Mybatis官网自己实现了:

  

<!--mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

 

  

 

配置,基本没有需要配置的:

# mybatis 别名扫描
mybatis.type-aliases-package=com.heima.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml

 

需要注意,这里没有配置mapper接口扫描包,因此我们需要给每一个Mapper接口添加@Mapper注解,才能被识别。

@Mapper// 为了把mapper这个DAO交給Spring管理 不需要配置mapper映射文件 mapper-locations: 
interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
}

  2>同用mapper 自动生成MySQL单表查询的基础语句

  使用方法:

  启动器:中包含了jdbc和mybatis的启动器

  

     <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>

 

 

 4.整合注解

 1>实体类的注解

@Data//自动生成get set方法
@Table(name = "tb_user")
public class User implements Serializable {
    @Id  //设置主键
    @KeySql(useGeneratedKeys = true) //主键自增长
    private Integer id;
    private Date updated;
    @Transient  //不需要持久化的字段
    private String note;
}

 

 

 5.如果不想每次都写private final Logger logger = LoggerFactory.getLogger(XXX.class); 可以用注解@Slf4j

  SLF4J所提供的核心API是一些接口以及一个LoggerFactory的工厂类。SLF4J提供了统一的记录日志的接口,只要按照其提供的方法记录即可,最终日志的格式、记录级别、输出方式等通过具体日志系统的配置来实现,因此可以在应用中灵活切换日志系统。

  启动器

       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

TZ_11_Spring-Boot的整合SpringMvc和MyBatis

原文:https://www.cnblogs.com/asndxj/p/11449014.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!