idea创建spring boot+mybatis(oracle)+themeleaf项目

1.新建项目

选择idea已经有的spring initializr

next,然后填写项目命名,包名

然后next,选择所需要的依赖

然后一路next,finish,项目新建成功,然后可以删除下面的三个文件和包,没卵用,删掉看的舒服

然后就是建项目结构,上面java包下的可以直接new--package,但是resources的直接new--directory会变成如下的长的的包名,所以需要设置一下,点击上面的那个小齿轮,然后将下图的设置的 √ 去掉,然后就可以直接new--directory就是树状结构了

完整的项目结构是这样的

2.添加依赖和其他配置

pom.xml:注释的是freemarker的模板依赖以及mysql的依赖,有需要的可以换成这个或其他的,然后还配了一些热部署,以及扫描xml的

  1.   

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>prvi.chen</groupId>
   <artifactId>springdemo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>spring-demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
</parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>1.3.1</version>
      </dependency>
       <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>net.sourceforge.nekohtml</groupId>
         <artifactId>nekohtml</artifactId>
         <version>1.9.22</version>
      </dependency>
      <!--<dependency>-->
         <!--<groupId>org.springframework.boot</groupId>-->
         <!--<artifactId>spring-boot-starter-freemarker</artifactId>-->
      <!--</dependency>-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc6</artifactId>
         <version>11.2.0.1.0</version>
      </dependency>
      <!--<dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
      </dependency>-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-pool2</artifactId>
         <version>2.2</version>
      </dependency>
      <dependency>
         <groupId>commons-io</groupId>
         <artifactId>commons-io</artifactId>
         <version>2.4</version>
      </dependency>
      <dependency>
         <groupId>org.codehaus.jackson</groupId>
         <artifactId>jackson-mapper-asl</artifactId>
         <version>1.9.13</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
      </dependency>
   </dependencies>

   <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.3.2</version>
            <configuration>
               <verbose>true</verbose>
               <overwrite>true</overwrite>
            </configuration>
         </plugin>
      </plugins>

      <resources>
         <resource>
            <directory>src/main/java</directory>
            <includes>
               <include>**/*.xml</include>
            </includes>
            <!-- 是否替换资源中的属性-->
            <filtering>false</filtering>
         </resource>
      </resources>
   </build>




</project>

SpringbootDemoApplication:程序入口,顺便配置扫描包,项目运行的时候直接run或者debug这个就行了


package com.jsiec;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.jsiec.mapper")
@ComponentScan({"com.jsiec.controller","com.jsiec.service"})
public class SpringbootDemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringbootDemoApplication.class, args);
   }
}


application.properties:这个是配置文件,可以配置数据库相关设置,包括数据库连接池设置(我没配连接池),然后还有一些模板加载页的配置之类的

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL
spring.datasource.username=DEMO
spring.datasource.password=123456789

########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
#页面热加载
spring.thymeleaf.cache=false

#修改tomcat的默认的端口号,将8080改为8888
server.port=8888


controller:几种传参方法,自己挑一种用用,里面的一些mapper,entity是mybatis自动生成的,就不放了

package priv.chen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.chen.entity.Test;
import priv.chen.entity.User;
import priv.chen.mapper.TestMapper;
import priv.chen.mapper.UserMapper;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/19.
 */
@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
private UserMapper userMapper;
    @Autowired
private TestMapper testMapper;
    @RequestMapping("/index")
    public Object index(){
        return "index";
    }

    @ResponseBody
    @RequestMapping("/hello")
    public Object hello(){
        return "say hello";
    }


    @RequestMapping("/world")
    public Object world(Model model) {
        model.addAttribute("name", "张一");
        return "/test/world";
    }

    @RequestMapping("/getUser")
    public Object getUser(Model model,User user){
        List<User> list = userMapper.getAll(user);
        Test test = testMapper.selectByPrimaryKey("1");
        model.addAttribute("list",list);
        model.addAttribute("test",test);
        return "/test/test";
    }

    @RequestMapping("/test1")
    public String test1(Test test){
        test = testMapper.selectByPrimaryKey("1");
        return "/test/test";
    }
    @RequestMapping("/test2")
    public String test2(Map<String,Object> map){
        map.put("name", "张二");
        return "/test/test";
    }
    @RequestMapping("/test3")
    public String test3(ModelMap modelMap){
        modelMap.addAttribute("name","张三");
        return "/test/test";
    }
    @RequestMapping("/test4")
    public String test4(Model model){
        model.addAttribute("name","张四");
        return "/test/test";
    }
    @RequestMapping("/test5")
    public String test5(ModelMap modelMap){
        modelMap.put("name","张五");
        return "/test/test";
    }
    @RequestMapping("/test6")
    public String test6(User user,Map< String, Object> map){
        List<User> list = userMapper.getAll(user);
        map.put("list",list);
        return "/test/test";
    }

}

然后就html页面,用的是themeleaf,注释的是用的其他传参方法

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<!--<div>
    姓名:<span th:text="${test.name}"></span><br/>
</div>-->
<!--<div>
    姓名:<span th:text="${name}"></span><br/>
</div>-->
<div th:each="user:${list}">
    姓名:<span th:text="${user.name}"></span><br/>
</div>
</body>

</html>

下面是运行效果:

最后放一张图

原文地址:https://www.cnblogs.com/qianzf/p/8393004.html