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. 实现word在线预览 有php的写法 也有插件似

    <?php //header("Content-type:text/html;charset=utf-8"); //word转html 展示 $lj=$_GET['file' ...

  2. C++ Standard Template Library (STL) 高级容器

    更多 STL 数据结构请阅读 NOIp 数据结构专题总结(STL structure 章节) std::map Definition: template < class Key, // map: ...

  3. [CSP-S模拟测试]:走格子(模拟+BFS+Dijkstra)

    题目描述 $CYJ$想找到他的小伙伴$FPJ$,$CYJ$和$FPJ$现在位于一个房间里,这个房间的布置可以看成一个$N$行$M$列的矩阵,矩阵内的每一个元素会是下列情况中的一种:$1.$障碍区域—这 ...

  4. html from表单异步处理

    from表单异步处理. 简单处理方法: jQuery做异步提交表单处理, 通过$("#form").serialize()将表单元素的数据转化为字符串, 最后通过$.ajax()执 ...

  5. Oracle系列:触发器、作业、序列、连接

    .Net程序员学用Oracle系列(8):触发器.作业.序列.连接   1.触发器 2.作业 2.1.作业调度功能和应用 2.2.通过 DBMS_JOB 来调度作业 3.序列 3.1.创建序列 3.2 ...

  6. 从零搭建一个Redis服务

    前言 自己在搭建redis服务的时候碰到一些问题,好多人只告诉你怎么成功搭建,但是并没有整理过程中遇到的问题,所有楼主就花了点时间来整理下. linux环境安装redis 安装中的碰到的问题和解决办法 ...

  7. Windows系统命令整理-Win10

    硬件相关 显卡 显卡升级 - 我的电脑->属性->设备管理器->显示适配器->更新驱动程序 服务 telnet 安装:启用或关闭Windows 功能,勾选上“Telnet客户端 ...

  8. Python的一些高级特性

    内容基本上来自于廖雪峰老师的blog相当于自己手打了一遍,加强加强理解吧. http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493 ...

  9. 进程池Pool的简单使用,同步异步的区别

    #进程池 """ 当需要创建子进程数量不多的时候,可以直接利用multiprocessing 中的Process动态生成多个进程,但是如果上百甚至上千个任务, " ...

  10. 排序算法三:堆排序(Heapsort)

    堆排序(Heapsort)是一种利用数据结构中的堆进行排序的算法,分为构建初始堆,减小堆的元素个数,调整堆共3步. (一)算法实现 protected void sort(int[] toSort) ...