pom文件里添加依赖

    <!-- 数据库需要的依赖 -->        
  
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

application.properties
我用的是springBoot 2.1 不需要配置驱动,配置驱动的话反而会出现警告
#数据库
spring.datasource.url=jdbc:mysql://192.168.238.130:3306/dev?useUniode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123


Mapper.java文件
import java.util.ArrayList;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import com.sc.Firstboot.entity.User; public interface MainMapper { /**
* 简单查询的话可以用下面这中注解的方式,比较简洁
* 复杂查询还是需要用到mapper.xml配置文件来完成
* @return
*/ @Select("select * from user")
public ArrayList<User> find(); @Insert("insert into user(id,username,age,pwd) values(#{id},#{username},#{age},#{pwd})")
public int insert(@Param("id") String id, @Param("username") String username, @Param("age") int age,
@Param("pwd") String pwd); public ArrayList<User> findAllAndPro();
}

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="com.sc.Firstboot.dao.MainMapper"> <select id="findAllAndPro" resultMap="userMap">
select
us.id,us.username,us.pwd,us.age,pru.id as pid ,
pru.productName,pru.price,pru.total
from user us
left join product pru on pru.uid = us.id
</select> <!-- 注意:当多个表的字段名一样的时候,查询需要用别名,否则查询结果不理想 -->
<resultMap id="userMap" type="com.sc.Firstboot.entity.User">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="pwd" column="pwd" />
<result property="age" column="age" />
<collection property="pList" ofType="com.sc.Firstboot.entity.Product">
<id property="id" column="pid" />
<result property="productName" column="productName" />
<result property="total" column="total" />
</collection>
</resultMap> </mapper>
service
@Service
public class MainService { @Autowired
private MainMapper mainMapper; public String insert(User user){
if(1 == mainMapper.insert("ewq", user.getUserName(),user.getAge(), user.getPwd()))return "success";
return "faild";
} public ArrayList<User> findAll(){
return mainMapper.find();
} public ArrayList<User> findAllAndPro(){
return mainMapper.findAllAndPro();
} }

controller

@Controller
public class MainController { @Autowired
private MainService mainSerice; /**
* 传参的时候需要注意区分命名,尽量不要使用相同的命名,
* 否则的框架会给形参列表中所有的实体里所有相同的属性名都赋值
* 例如 User实体和Product实体中都有id属性
* 那么传参的时候如果 id=XXX 那么两个实体的id都会被赋值XXX
* @param user
* @param product
* @return
*/
@ResponseBody
@RequestMapping(path = {"/insertUser"}, method = {RequestMethod.GET, RequestMethod.POST})
public String insertUser(User user,Product product){
System.out.println(user.toString());
System.out.println(product.toString());
return mainSerice.insert(user);
} @ResponseBody
@RequestMapping(path = {"/findAll"}, method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
Map<String,Object> map = new HashMap<>();
map.put("userList",mainSerice.findAll());
mv.setViewName("login/login");
mv.addObject("result",map);
return mv;
} @ResponseBody
@RequestMapping(path = {"/findAllAndPro"}, method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView findAllAndPro(){
ModelAndView mv = new ModelAndView();
Map<String,Object> map = new HashMap<>();
map.put("userList",mainSerice.findAllAndPro());
mv.setViewName("login/login");
mv.addObject("result",map);
return mv;
}
}

Application.java

/**
* 启动mapper文件的扫描注解
* 他会去制定的包范围下扫描所有以Mapper接吻的java文件
* 默认扫整个项目
* @author lenovo
*
*/
@SpringBootApplication
@MapperScan(basePackages={"com.sc.myProject.dao"})
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Spring Boot学习笔记(五)整合mybatis的更多相关文章

  1. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  2. Spring Boot 知识笔记(整合Mybatis)

    一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...

  3. Spring Boot学习笔记:整合Shiro

    Spring Boot如何和Shiro进行整合: 先自定义一个Realm继承AuthorizingRealm,并实现其中的两个方法,分别对应认证doGetAuthenticationInfo和授权do ...

  4. Spring Boot学习笔记:整合H2数据库

    H2数据库:java语言编写的嵌入式sql数据库.可以和应用一起打包发布. H2有三种连接模式(Connection Modes): Embedded mode (local connections ...

  5. Spring Boot 知识笔记(整合Mybatis续-补充增删改查)

    续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...

  6. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  7. Spring Boot 2.X(五):MyBatis 多数据源配置

    前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...

  8. Spring Boot 学习笔记--整合Thymeleaf

    1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...

  9. spring boot整合jsp的那些坑(spring boot 学习笔记之三)

    Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency>            <groupId>or ...

随机推荐

  1. 【ocp-12c】最新Oracle OCP-071考试题库(45题)

    45.(9-16)choose the best answer: View the Exhibit and examine the data in the EMPLOYEES table. You w ...

  2. BZOJ 3110 [Zjoi2013]K大数查询 (CDQ分治+树状数组)

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...

  3. C++中指针运算

    1,指针可以和数字运算,指针+-整数,如, int num[] = {1,2,3,4,5,6,7,8}; int *p = num; p++; p--; p = p + 3; p = p -3; 数字 ...

  4. jmeter+ant+jenkins+mac报告优化(一):解决Min Time和Max Time显示NaN

    一.在上篇博客中生成的报告有两个问题: 1.date not defined 2.Min Time和Max Time显示成了NaN 二.Jmeter+Ant报告生成原理: 1.在Jmeter的extr ...

  5. [CSS3] 边栏导航动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 【vue】——使用watch 观察路由变化,重新获取内容

    更新:11-29 时隔半年,又重新使用VUE进行开发,有了新方案--beforeRouteLeave 在组件内直接使用,前提是你用了vue-router: beforeRouteLeave (to, ...

  7. day04 --class --homework

    # -*- coding: utf-8 -*- # @Time : 2018/12/24 12:10 # @Author : Endless-cloud # @Site : # @File : 04 ...

  8. 第一个PSP0级

    1.计划: 需求描述: 按照图片要求设计添加新课程界面.(0.5分) 在后台数据库中建立相应的表结构存储课程信息.(0.5分) 实现新课程添加的功能. 要求判断任课教师为王建民.刘立嘉.刘丹.王辉.杨 ...

  9. 数据结构基础 ---- 数组的理解和实现(Java)

    什么是数组 数组是由类型相同的数据元素构成的有序集合,每个元素称为数组元素,每个元素受n(n>= 1)个线性关系的约束,每个元素在n个线性关系中的序号i1, i2, ....., in称该元素的 ...

  10. 40.oracle事务

    一.事务特性 事务必须具备以下四个特性,简称ACID属性 原子性(Atomicity):事务是一个完整的操作.事务的各步操作是不可分割的(原子的):要么都执行,要么都不执行场景:银行转账 A-100 ...