依赖包:

 <?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. go struct{}的几种特殊用法

    参考:https://blog.csdn.net/kturing/article/details/80557280 1.声明为声明为map[string]struct{} 由于struct{}是空,不 ...

  2. Unity编辑器:基于NGUI的引用检测工具

    这里共享一个基于NGUI的引用检测工具.工具包括几个部分:Atlas/Sprite的引用查找:字库引用查找:UITexture引用查找:Component查找: 代码就不多介绍了,文章底部提供源码下载 ...

  3. Js获取当前页面URL各种参数

    JS获取当前页面URL各种参数 一:Location Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.locatio ...

  4. 针对2017年淘宝开放平台应用整改被封停或强制入塔政策实现不入塔不模糊正常调用API的解决方案

    淘宝开放平台入驻先是限制上架,提高入驻资质,然后又模糊化R2信息,强制入塔,如今开始大规模整改应用. 此次整改势必导致很大一批个人开发的应用无法使用. 在此本人有偿提供正常调用淘宝开放平台API的解决 ...

  5. python第三章:循环语句--小白博客

    Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非 ...

  6. K 班1-7,alpha,beta 作业成绩汇总

    K 班1-7,alpha,beta 作业成绩汇总 千帆竞发 详细得分 短学号 名 1 2 3 4 5 6 7 alpha beta TOTAL 505 基智 4.55 1 -2 0 0 -10 4.3 ...

  7. 行政区划sql数据脚本

    行政区划sql数据脚本 IF (EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TB_Province]') ...

  8. iOS基于B站的IJKPlayer框架的流媒体探究

    阅读数:6555 学习交流及技术讨论可新浪微博关注:极客James 一.流媒体 流媒体技术从传输形式上可以分为:渐进式下载和实施流媒体. 1.渐进式下载 它是介于实时播放和本地播放之间的一种播放方式, ...

  9. 并发包学习之-atomic包

    一,模拟并发代码: 线程不安全的代码 //并发模拟代码 public class CountExample { //请求总数 public static int clientTotal = 5000; ...

  10. jenkins 插件介绍

    1.jenkins 利用maven编译,打包,所需插件:Maven Integration: Maven集成插件这个插件提供了Jenkins和Maven的深度集成,无论是好还是坏:项目之间的自动触发取 ...