使用篇-springboot+mybatis+freemarker使用实例分享

  • 导入项目依赖
<dependencies>
        <dependency>
            <groupId>tk.mybatisgroupId>
            <artifactId>mapper-spring-boot-starterartifactId>
            <version>2.1.5version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
  • 加logback日志


<configuration>
    
    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />
    
    <property name="CONSOLE_LOG_PATTERN"
              value="时间:%date{yyyy-MM-dd HH:mm:ss} 级别: %highlight(%-5level) 进程: %boldYellow(%thread) 类: %boldGreen(%logger) 信息: %msg%n"/>
    
    <property name="ERRINFOFILE_LOG_PATTERN" value="时间:%date{yyyy-MM-dd HH:mm:ss} 级别: %-5level 进程: %thread 类: %logger 信息: %msg%n"/>

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                ${CONSOLE_LOG_PATTERN}
            pattern>
        layout>
    appender>

    <root level="info">
        <appender-ref ref="consoleLog" />
    root>
configuration>
  • 代码分层

    • bean

      • 数据库表(t_user)

      使用篇-springboot+mybatis+freemarker使用实例分享_第1张图片

      • JavaBean

        @Data
        @Table(name = "t_user")
        public class User implements Serializable {
                   
            @Column(name = "u_id")
            private Integer id;
            @Column(name = "u_name")
            private String name;
            @Column(name = "u_password")
            private String password;
        }
        
    • mapper

      public interface UserMapper extends Mapper<User> {
               
      }
      
    • service

      public interface UserService {
               
          List<User> listAll();
      }
      
      //实现类
      @Service
      public class UserServiceImpl implements UserService {
               
      
          @Autowired
          private UserMapper userMapper;
      
          @Override
          public List<User> listAll() {
               
              return userMapper.selectAll();
          }
      }
      
    • controller

      @Controller
      public class UserController {
               
          @Autowired
          private UserService userService;
          
          @RequestMapping("/list")
          public String listAll(Model model){
               
              List<User> userList = userService.listAll();
              model.addAttribute("list",userList);
              return "list";
          }
      }
      
  • 配置文件

    • application.yml

      spring:
        datasource:
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql:///db4?serverTimezone=GMT%2B8
          username: root
          password: root
        mvc:
          view:
            prefix: /
            suffix: .ftl
        freemarker:
          suffix: .ftl
          content-type: text/html
          charset: UTF-8
      
      mybatis:
        mapper-locations: classpath:mappers/*.xml
        type-aliases-package: com.ujiuye.bean
      
  • 模板引擎页面

    list.ftl

    DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        <h2>用户列表h2>
        <#list list as user>
            <ul>
                <li>${user.id}li>
                <li>${user.name}li>
                <li>${user.password}li>
            ul>
        #list>
    body>
    html>
    
  • 测试
    使用篇-springboot+mybatis+freemarker使用实例分享_第2张图片

你可能感兴趣的:(SpringBoot,spring,boot,java)