SpringBoot是用来简化SpringMvc开发的项目,这里自然要整合mybatis等持久化框架!

先看看项目目录:

一、在pom.xml中配置依赖jar包:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mfc</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot Maven Webapp</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</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>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>

提示:springboot热部署配置:

热部署:修改java代码后直接保存即可在浏览器上运行,不需要手动重启服务器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

二、项目入口:
package com.mfc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

三、控制器类:
package com.mfc.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.mfc.dao.UserMapper;
import com.mfc.entity.User;

@RestController
@RequestMapping("/userController")
public class UserController {

@Autowired
private UserMapper userMapper;

@RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
public ModelAndView selectUserById(String id){
User user = userMapper.selectUserById(Integer.parseInt(id));
ModelAndView mav = new ModelAndView("updateUser");
mav.addObject("user", user);
return mav;
}

@RequestMapping(value={"/selectAllUser"}, method=RequestMethod.GET)
public ModelAndView selectAllUser(){
List<User> list = userMapper.selectAllUser();
ModelAndView mav = new ModelAndView("alluser");
mav.addObject("users", list);
return mav;
}

@RequestMapping(value={"/addUserPage"}, method=RequestMethod.GET)
public ModelAndView addUserPage(){
ModelAndView mav = new ModelAndView("adduser");
return mav;
}

@RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
public ModelAndView addUser(User user){
userMapper.addUser(user);
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}

@RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
public ModelAndView updateUser(User user){
userMapper.updateUser(user);
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}

@RequestMapping(value={"/deleteUser"}, method=RequestMethod.GET)
public ModelAndView deleteUser(String id){
userMapper.deleteUser(Integer.parseInt(id));
ModelAndView mav = new ModelAndView("redirect:selectAllUser");
return mav;
}
}

四、资源配置文件:application.properties
#配置数据库连接数据
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#配置控制器里面返回页面的前后缀
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

五、dao层以及service层和springmvc中一样使用,实体类省略!
这里的dao层使用注解实现:

package com.mfc.dao;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.mfc.entity.User;

@Mapper
public interface UserMapper {

@Select("select * from user where id = #{id}")
public User selectUserById(int id);

@Select("select * from user")
public List<User> selectAllUser();

@Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
public void addUser(User user);

@Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
public void updateUser(User user);

@Delete("delete from user where id=#{id}")
public void deleteUser(int id);

}

springboot+jsp+mybatis项目实例(后台成功,但是无法跳转jsp页面,没有实体类的注解,看springboot+jsp第二弹相关配置,即可成功配置jsp)的更多相关文章

  1. springboot + swagger的实体类属性注解

    @Api:用在类上,说明该类的作用 @ApiOperation:用在方法上,说明方法的作用 @ApiImplicitParams:用在方法上包含一组参数说明 @ApiImplicitParam:用在@ ...

  2. 阶段3 1.Mybatis_09.Mybatis的多表操作_4 完成account一对一操作-建立实体类关系的方式

    定义user的实体.然后生成getter和setter 定义一个可以封装Account和User的Map type这里虽然是account类型 这一段只能保证account的数据完成.并不能保证use ...

  3. springboot 整合 mybatis 入门

    springboot整合mybatis 0.yml 配置文件 1.创建数据库表. 2.创建实体类. 3.创建 Mapper 接口 ,添加 @Mapper 注解. 4.创建 Mapper 映射文件. & ...

  4. SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(一): 搭建基本环境、整合 Swagger、MyBatisPlus、JSR303 以及国际化操作

    相关 (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y- ...

  5. SpringBoot集成Mybatis实现多表查询的两种方式(基于xml)

     下面将在用户和账户进行一对一查询的基础上进行介绍SpringBoot集成Mybatis实现多表查询的基于xml的两种方式.   首先我们先创建两个数据库表,分别是user用户表和account账户表 ...

  6. springboot整合mybatis与thymeleaf

    1.创建springboot项目 (1)选择Spring Initializr (2)填写自己的Group 与 Artifact (3)选择依赖框架 等待maven下载好依赖和插件即可 2.主配置文件 ...

  7. SpringBoot 整合 MyBatis,实现 CRUD 示例

    目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...

  8. SpringBoot整合Mybatis使用注解或XML的方式开发

    2018-6-4 补充mybatis-spring-boot注解的使用 1.导包 只需要再导入mysql+mybatis两个包 <dependency> <groupId>or ...

  9. springboot系列七:springboot 集成 MyBatis、事物配置及使用、druid 数据源、druid 监控使用

    一.MyBatis和druid简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.M ...

随机推荐

  1. 【HDOJ6614】AND Minimum Spanning Tree(签到)

    题意:给定标号从1到n的n个点,链接两个点x,y的代价为x AND y,求最小生成树总代价与满足代价最小的前提下字典序最小的方案 n<=2e5 思路: #include<bits/stdc ...

  2. python保存selenium的cookies写入和读出

    def write_cookie(self, cookie): try: with open("cookies%s" % self.uid, "wb+") as ...

  3. 4期Web安全基础

    介绍了web安全的各种常见漏洞.视频卡顿,建议直接看网易出品的白帽子视频. 类似的教程还有,网易白帽子的教程:参考简书https://www.jianshu.com/p/1b372ca96b87 在看 ...

  4. EasyUI的columns中列标题居中

    $("#supDataList").datagrid({   url: "../Ajax/SupplierAjax.ashx",   queryParams:  ...

  5. springboot启动脚本

    #!/bin/sh JAVA_HOME="/ulic1/jdk/jdk1.8.0_201/bin" export JAVA_HOME lsof -i:9010 |awk '{pri ...

  6. python datetime模块的strftime()

    strftime()   可以对datetime对象进行格式化,生成需要时间格式的时间 strptime()  可以对格式化后的时间再生成datetime对象 格式化时间时,如果不想要-来隔开,还可以 ...

  7. Vue2.0响应式原理以及重写数组方法

    // 重写数组方法 let oldArrayPrototype = Array.prototype; let proto = Object.create(oldArrayPrototype); ['p ...

  8. JavaScript.convertArray

    function convertArray(nodeList){     var arr = []     if(Array.prototype.slice){         arr = [].sl ...

  9. jvm 这我就能会了 擦

    最近老有人问jvm,恕我直言,完蛋了,不会,慢慢学吧,开始第一个学习,后续补充,走起... 我看的他的https://www.cnblogs.com/dingyingsi/p/3760447.html ...

  10. 探究activity-android学习第三天

    转载来源:https://www.jianshu.com/p/f8c68b3c2847?utm_campaign=maleskine&utm_content=note&utm_medi ...