Spring boot

1.创建spring boot项目
Spring boot_第1张图片
注意packaging选着War
Spring boot_第2张图片
选着Web
Spring boot_第3张图片

Spring boot_第4张图片
创建完成后的目录如下所示
Spring boot_第5张图片
2.编写springboot程序
在zhongruan下创建bean包,并创建UserInfo

package com.zhongruan.bean;

public class UserInfo {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

在zhongruan下创建dao包,并创建IUserInfoDao

package com.zhongruan.dao;

import com.zhongruan.bean.UserInfo;

public interface IUserInfoDao {
    void add(UserInfo userInfo);
}

在resources包下创建mapper包,并创建UserInfoMapper





    
        insert into userinfo(username,password) values (#{username},#{password})
    

在zhongruan包下创建service包,并创建IUserInfoService

package com.zhongruan.service;

import com.zhongruan.bean.UserInfo;

public interface IUserInfoService {
    void add(UserInfo userInfo);
}

在service包下创建impl包,并创建UserInfoService

package com.zhongruan.service.impl;

import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserInfoDao;
import com.zhongruan.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService implements IUserInfoService {

    @Autowired
    private IUserInfoDao userInfoDao;
    @Override
    public void add(UserInfo userInfo) {
        userInfoDao.add(userInfo);
    }
}

在templates下建立input.html





    
    增加信息的页面


        
用户
密码

和ok.html




    
    操作成功页面


    恭喜!!


编写application.properties连接数据库


spring.datasource.data-username=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/user?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.type-aliases-package=com.zhongruan.bean
mybatis.mapper-locations=classpath*:mapper/*.xml

在pom.xml下添加依赖



    org.springframework.boot
    spring-boot-starter-thymeleaf




    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.1.1



    mysql
    mysql-connector-java
    runtime



    com.alibaba
    druid
    1.0.9


在zhongruan下创建controller包HelloController

package com.zhongruan.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class HelloController {

    @RequestMapping("hello.do")
    @ResponseBody
    public String hello(){
        return "hello spring boot";
    }

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

和UserInfoController

package com.zhongruan.controller;

import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @RequestMapping("toAdd.do")
    public String toAdd(){
        return "input";
    }

    @RequestMapping("add.do")
    public String add(UserInfo userInfo){
        userInfoService.add(userInfo);
        return "ok";
    }
}

在zhongruan包下创建App.java

package com.zhongruan;

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.zhongruan.dao")
public class App {
    public static void main(String[] args){
        SpringApplication.run(App.class,args);
    }
}

3.运行程序
运行App.java
Spring boot_第6张图片
打开浏览器输入http://localhost:8080/user/toAdd.do
Spring boot_第7张图片
Spring boot_第8张图片
Spring boot_第9张图片
Spring boot_第10张图片
到此,一个简单的springboot程序完成了

你可能感兴趣的:(Spring boot)