依赖包:

 <?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.springboot</groupId>
<artifactId>demo</artifactId>
<version>1.0.1-SNAPSHOT</version>
<name>boot_demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<dom4j.version>1.6.1</dom4j.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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <!-- 引入freemarker包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- spring事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency> <dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build> </project>

配置文件application.properties:

#数据库配置
spring.datasource.url=jdbc:mysql://20.1.1.11:16306/gxrdb?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassNamee=com.mysql.jdbc.Driver #mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=org.springboot.model

model:

 package org.springboot.model;

 /**
* @Auther:GongXingRui
* @Date:2018/12/24
* @Description:
**/
public class Student {
private String id;
private String name;
private String gender;
private int age;
private String telephone; public Student() { } public Student(String id, String name, String gender) {
this.id = id;
this.name = name;
this.gender = gender;
} public Student(String id, String name, String gender, int age, String telephone) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.telephone = telephone;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} @Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", telephone='" + telephone + '\'' +
'}';
}
}

mapper接口

 package org.springboot.mapper;

 import org.apache.ibatis.annotations.Mapper;
import org.springboot.model.Student; import java.util.List; /**
* @Auther:GongXingRui
* @Date:2018/12/26
* @Description:
**/
@Mapper
public interface StudentMapper {
Student getStudentById(String id); List<Student> getAllStudent(); void addStudent(Student student); void deleteStudentById(String id); }

mapper XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.springboot.mapper.StudentMapper"> <!--<resultMap id="BaseResultMap" type="org.springboot.model.Student" >-->
<!--<id column="id" property="id" jdbcType="VARCHAR" />-->
<!--<result column="name" property="name" jdbcType="VARCHAR" />-->
<!--<result column="age" property="age" jdbcType="INTEGER" />-->
<!--<result column="gender" property="gender" jdbcType="VARCHAR"/>-->
<!--<result column="telephone" property="telephone" jdbcType="VARCHAR"/>-->
<!--</resultMap>--> <!--<select id="getStudentById" parameterType="java.lang.String" resultMap="BaseResultMap">-->
<!--select * from t_student where id = #{id}-->
<!--</select>--> <select id="getStudentById" parameterType="java.lang.String" resultType="org.springboot.model.Student">
select * from t_student where id = #{id}
</select> <insert id="addStudent" parameterType="org.springboot.model.Student">
insert into t_student (id, name, gender, age,telephone) values (#{id} ,#{name}, #{gender},#{age,jdbcType=INTEGER},#{telephone})
</insert> <!--字段可能为空的情况,需要指定数据类型-->
<!--<insert id="addStudent" parameterType="org.springboot.model.Student">-->
<!--insert into t_student (id, name, gender, age,telephone) values (#{id} ,#{name}, #{gender},#{age,jdbcType=INTEGER},#{telephone,jdbcType=CHAR})-->
<!--</insert>--> <delete id="deleteStudentById" parameterType="java.lang.String">
DELETE FROM t_student WHERE id = #{id}
</delete> <select id="getAllStudent" resultType="org.springboot.model.Student">
select * from t_student
</select> </mapper>

service:

 package org.springboot.service;

 import org.springboot.mapper.StudentMapper;
import org.springboot.model.Student;
import org.springboot.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; /**
* @Auther:GongXingRui
* @Date:2018/12/24
* @Description:
**/
@Service
public class StudentService { @Autowired
private StudentMapper studentMapper; public Student getStudentDemo() {
Student student = new Student();
student.setId("1");
student.setName("小明");
student.setGender("男");
student.setAge(18);
student.setTelephone("137456678");
return student;
} public Student getStudentById(String id) {
return studentMapper.getStudentById(id);
} public List<Student> getAllStudent() {
return studentMapper.getAllStudent();
} public void addStudent(Student student) {
studentMapper.addStudent(student);
} public void deleteStudentById(String id) {
studentMapper.deleteStudentById(id);
} public boolean loginStudentcheck(String name, String id) {
Student student = studentMapper.getStudentById(id);
if (ObjectUtils.isNull(student)) {
return false;
} else if (student.getName().equals(name)) {
return true;
}
return false;
} }

controller

 package org.springboot.controller;

 import com.sun.jmx.snmp.SnmpUnknownModelLcdException;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springboot.mapper.StudentMapper;
import org.springboot.model.Student;
import org.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; import java.util.List; /**
* @Auther:GongXingRui
* @Date:2018/12/24
* @Description:
**/
@Controller
public class StudentController {
private Logger logger = LogManager.getLogger(StudentController.class);
@Autowired
StudentService studentService; @RequestMapping("/showStudentInfo")
public String showStudentInfo(Model model) {
logger.info("查询学生信息");
Student student = studentService.getStudentDemo();
logger.info(student);
model.addAttribute("student", student);
return "studentInfo.html";
} // 使用freemaker模板
@RequestMapping("/showStudentInfo2")
public String showStudentInfo2(Model model) {
logger.info("查询学生信息");
Student student = studentService.getStudentDemo();
logger.info(student);
model.addAttribute("student", student);
return "studentInfo2";
} @RequestMapping("/studentIndex")
public String studentIndex() {
logger.info("学生信息首页");
return "student/studentIndex";
} @RequestMapping("/getStudentById")
@ResponseBody
public String getStudentById(String id) {
logger.info("通过ID查询学生信息");
Student student = studentService.getStudentById(id);
if (null == student) {
String tip = "没有找到对应数据 -> id:" + id;
logger.info(tip);
return tip;
}
logger.info(student);
return student.toString();
} @RequestMapping("/getStudentById2")
public String getStudentById2(String id, Model model) {
logger.info("通过ID查询学生信息2");
Student student = studentService.getStudentById(id);
if (null == student) {
String tip = "没有找到对应数据 -> id:" + id;
logger.info(tip);
return null;
}
model.addAttribute("student", student);
logger.info(student);
return "student/studentInfo";
} @RequestMapping("/getAllStudent")
public ModelAndView getAllStudent(ModelAndView mv) {
logger.info("查询所有学生信息");
List<Student> students = studentService.getAllStudent();
mv.addObject("studentList", students);
mv.setViewName("student/allStudentInfo");
logger.info(students);
return mv;
} @RequestMapping("/addStudent")
@ResponseBody
public String addStudent(Student student) {
logger.info("新增学生信息");
try {
studentService.addStudent(student);
} catch (Exception e) {
e.printStackTrace();
return "新增学生信息发生异常!<br>" + e.toString();
}
logger.info(student.toString());
return "新增学生信息成功!<br>" + student.toString();
} @RequestMapping("/deleteStudentById")
@ResponseBody
public String deleteStudentById(String id) {
logger.info("通过ID删除学生信息");
try {
studentService.deleteStudentById(id);
} catch (Exception e) {
e.printStackTrace();
return "通过ID删除学生信息发生异常!<br>" + e.toString();
}
logger.info("通过ID删除学生信息成功! -> id:" + id);
return "通过ID删除学生信息成功! -> id:" + id;
} @RequestMapping("/loginStudentcheck")
public String loginStudentcheck(String name, String id, Model model) {
logger.info("正在登陆学生系统...");
logger.info("用户名:" + name + ", 密码:" + id);
boolean flag = studentService.loginStudentcheck(name, id);
if (flag) {
logger.info("登陆成功");
return "student/studentIndex";
}
model.addAttribute("info", "登陆失败!用户名或密码错误!");
logger.info("登陆失败!用户名或密码错误!");
return "student/error";
} @RequestMapping("/loginStudent")
public String loginStudent() {
logger.info("请求登陆学生信息页面");
return "student/loginStudent";
} }

程序入口:DemoApplication.java

 package org.springboot;

 import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("org.springboot.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

工程目录:

SpringBoot之整合Mybatis范例的更多相关文章

  1. SpringBoot之整合Mybatis(增,改,删)

    一,在上一篇文章SpringBoot之整合Mybatis中,我们使用spring boot整合了Mybatis,并演示了查询操作.接下来我们将完善这个示例,增加增,删,改的功能. 二,改动代码 1.修 ...

  2. SpringBoot系列-整合Mybatis(注解方式)

    目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...

  3. springboot(二)整合mybatis,多数据源和事务管理

     -- 1.整合mybatis -- 2.整合多数据源 -- 3. 整合事务 代码地址:https://github.com/showkawa/springBoot_2017/tree/master/ ...

  4. SpringBoot系列-整合Mybatis(XML配置方式)

    目录 一.什么是 MyBatis? 二.整合方式 三.实战 四.测试 本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程. 一.什么是 MyBatis? MyBatis 是一款优 ...

  5. 【springboot】整合 MyBatis

    转自:https://blog.csdn.net/cp026la/article/details/86493503 1. 简介: 目前,国内大部分公司都使用 MyBatis作为持久层框架.本章整合My ...

  6. SpringBoot (四) - 整合Mybatis,逆向工程,JPA

    1.SpringBoot整合MyBatis 1.1 application.yml # 数据源配置 spring: datasource: driver-class-name: com.mysql.c ...

  7. SpringBoot之整合MyBatis

    今天了解一下SpringBoot如何与我们最常用的ORM框架Mybatis整合. 一. 需要在pom.xml文件里加入mybatis的依赖 <dependency> <groupId ...

  8. 玩转SpringBoot之整合Mybatis拦截器对数据库水平分表

    利用Mybatis拦截器对数据库水平分表 需求描述 当数据量比较多时,放在一个表中的时候会影响查询效率:或者数据的时效性只是当月有效的时候:这时我们就会涉及到数据库的分表操作了.当然,你也可以使用比较 ...

  9. springboot项目整合mybatis

    记录创建springboot项目并配置mybatis中间件: 资源准备及版本说明 编程工具:IDEA JDK版本:1.8 Maven版本:Apache Maven 3.6.3 springboot版本 ...

随机推荐

  1. 五.JQuary 实例

    需要引入jquery.js文件 script type="text/javascript" src="../js/jquery-3.2.1.js">< ...

  2. [ASP.NET]ScriptManager控件使用 转载

    目录 概述 局部刷新 错误处理 类型系统扩展 注册定制脚本 注册 Web 服务 在客户端脚本中使用认证和个性化服务 ScriptManagerProxy 类 添加 ScriptManager 控件 客 ...

  3. object detection[SSD]

    0. 背景 经过了rcnn,spp,fast rcnn, faster rcnn,yolo,这里又到了ssd模型. faster rcnn的贡献是将候选框区域提取的部分也集成到CNN中去,并且与对象的 ...

  4. UINavigationController - BNR

    继续上篇UITableView的编辑操作. 当你初始化一个UINavigationController对象时,它将拥有一个根视图控制器,即UIViewController.根视图控制器一直存在于sta ...

  5. flask 跨域请求

    Flask中,跨域请求主要有两种方式: 1.在响应头信息中添加允许跨域 如下,使用装饰器app.after_request(我这里的web是定义的蓝图),这样在每次请求后,加入header 2.使用第 ...

  6. filebeat 源码编译安装

    下载filebeat源码(6.2.3)下载地址:链接: https://pan.baidu.com/s/1cPR7-xlQJuYZ77uaUpfSpQ 提取码: k77u github下载地址:htt ...

  7. docker创建nginx+php-fpm+mysql环境(一分钟搭建lnmp)

    下载镜像 docker pull bitnami/php-fpm #下载php-fpm镜像 docker pull nginx #下载nginx镜像docker pull mysql:5.5.59 # ...

  8. 详解ES5和ES6的继承

    ES5继承 构造函数.原型和实例的关系:每一个构造函数都有一个原型对象,每一个原型对象都有一个指向构造函数的指针,而每一个实例都包含一个指向原型对象的内部指针, 原型链实现继承 基本思想:利用原型让一 ...

  9. 动态规划-数位dp

    大佬讲的清楚 [https://blog.csdn.net/wust_zzwh/article/details/52100392] 例子 不要62或4 l到r有多少个数不含62或者4 代码 #incl ...

  10. C#跨进程读取listview控件中的数据

    http://www.cnblogs.com/Charltsing/p/slv32.html 欢迎交流:QQ564955427 读取标准的32位listview控件中的数据,网上已经有很多代码了.今天 ...