SpringBoot快速入门-三分钟集成mybatis和logback

快速生成项目框架

spring官方代码生成器

集成mybatis-generate插件

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.4.0</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<!--这里添加了mysql的驱动依赖,就不需要再配置文件中配置了-->
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.44</version>
					</dependency>
				</dependencies>

			</plugin>
		</plugins>

	</build>

application配置文件

logging.level.root=WARN
logging.level.com.baeldung=TRACE
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/wechat?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456


mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=com.bugcodes.bugcodermybatis.dao.model

集成日志logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOGS" value="./logs" />

    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/spring-boot-logger.log</file>
        <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.baeldung*" at TRACE level -->
    <logger name="com.bugcodes" level="info" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

编写测试用例

启动测试

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.4.10)

2021-09-19 23:18:17,630 INFO  [main] org.springframework.boot.StartupInfoLogger: Starting IUserServiceTest using Java 1.8.0_111 on bogon with PID 35733 (started by bugcoder in /Users/bugcoder/Downloads/bugcoder-mybatis)
2021-09-19 23:18:17,635 INFO  [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2021-09-19 23:18:19,967 INFO  [main] org.springframework.boot.StartupInfoLogger: Started IUserServiceTest in 2.826 seconds (JVM running for 3.801)
2021-09-19 23:18:20,486 INFO  [main] com.bugcodes.bugcodermybatis.service.IUserServiceTest: 查询结果UserModel [Hash = -1140143377, id=zhangsan, username=张三, password=123456, faceImage=face_zhangsan, faceImageBig=face_zhangsan_big, nickname=三儿, qrcode=二维码, cid=null, serialVersionUID=1]

 

完整源码

springboot集成mybatis-generate和logbackicon-default.png?t=L892https://download.csdn.net/download/m0_60992470/23657917​​​更多资源可以在我的资源中获取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进阶架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值