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

1.新建项目

idea创建spring boot+mybatis(oracle)+themeleaf项目_第1张图片

选择idea已经有的spring initializr

idea创建spring boot+mybatis(oracle)+themeleaf项目_第2张图片

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

idea创建spring boot+mybatis(oracle)+themeleaf项目_第3张图片

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

idea创建spring boot+mybatis(oracle)+themeleaf项目_第4张图片

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

idea创建spring boot+mybatis(oracle)+themeleaf项目_第5张图片

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

idea创建spring boot+mybatis(oracle)+themeleaf项目_第6张图片idea创建spring boot+mybatis(oracle)+themeleaf项目_第7张图片

完整的项目结构是这样的

idea创建spring boot+mybatis(oracle)+themeleaf项目_第8张图片

2.添加依赖和其他配置

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

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.0modelVersion>

   <groupId>prvi.chengroupId>
   <artifactId>springdemoartifactId>
   <version>0.0.1-SNAPSHOTversion>
   <packaging>jarpackaging>

   <name>spring-demoname>
   <description>Demo project for Spring Bootdescription>

   <parent>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-parentartifactId>
      <version>1.5.9.RELEASEversion>
      <relativePath/> 
   parent>

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

   <dependencies>
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-jdbcartifactId>
      dependency>
      <dependency>
         <groupId>org.mybatis.spring.bootgroupId>
         <artifactId>mybatis-spring-boot-starterartifactId>
         <version>1.3.1version>
      dependency>
       <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-thymeleafartifactId>
      dependency>
      <dependency>
         <groupId>net.sourceforge.nekohtmlgroupId>
         <artifactId>nekohtmlartifactId>
         <version>1.9.22version>
      dependency>
      
         
         
      
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-webartifactId>
      dependency>

      <dependency>
         <groupId>com.oraclegroupId>
         <artifactId>ojdbc6artifactId>
         <version>11.2.0.1.0version>
      dependency>
      
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-testartifactId>
         <scope>testscope>
      dependency>
      <dependency>
         <groupId>org.apache.commonsgroupId>
         <artifactId>commons-pool2artifactId>
         <version>2.2version>
      dependency>
      <dependency>
         <groupId>commons-iogroupId>
         <artifactId>commons-ioartifactId>
         <version>2.4version>
      dependency>
      <dependency>
         <groupId>org.codehaus.jacksongroupId>
         <artifactId>jackson-mapper-aslartifactId>
         <version>1.9.13version>
      dependency>
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-devtoolsartifactId>
         <optional>trueoptional>
      dependency>
   dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
         plugin>

         <plugin>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-maven-pluginartifactId>
            <version>1.3.2version>
            <configuration>
               <verbose>trueverbose>
               <overwrite>trueoverwrite>
            configuration>
         plugin>
      plugins>

      <resources>
         <resource>
            <directory>src/main/javadirectory>
            <includes>
               <include>**/*.xmlinclude>
            includes>
            
            <filtering>falsefiltering>
         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= 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,注释的是用的其他传参方法

html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Titletitle>
head>
<body>


<div th:each="user:${list}">
    姓名:<span th:text="${user.name}">span><br/>
div>
body>

html>

下面是运行效果:

idea创建spring boot+mybatis(oracle)+themeleaf项目_第9张图片

最后放一张图

idea创建spring boot+mybatis(oracle)+themeleaf项目_第10张图片

 
  
 
 

你可能感兴趣的:(oracle,idea,mybatis,spring)