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. 如何学习sql语言?

    如何学习 SQL 语言? https://www.zhihu.com/question/19552975 没有任何基础的人怎么学SQL? https://www.zhihu.com/question/ ...

  2. 【Qt】Qt在Windows下的开发与调试环境配置

    前文已经交待了从源码编译Qt自定义版本.现在可以开始配置一下开发与调试程序并写个Hello World了. 1. IDE 虽然Qt官方有VS插件使我们可以在VisualStudio中开发Qt应用,但我 ...

  3. 继承Runnable 实现Synchronized 同步锁

    在java编程中,经常需要用到同步,而用得最多的也许是synchronized关键字了,下面看看这个关键字的用法. 因为synchronized关键字涉及到锁的概念,所以先来了解一些相关的锁知识. j ...

  4. (RaspBerry Pi) Python GPIO 基本操作

    目前打算由潛入深慢慢學習RaspBerry Pi, 所以先由最容易下手的Python進入樹莓派的世界 首先要使用 GPIO 需要利用RPI.GPIO package想當然爾必須先安裝 所以先執行下列命 ...

  5. (JAVA作业)练习:创建一个类名为Fruit;包含实例变量:水果名称,颜色,价格,上市月份,有无种子 10个实例:苹果,香蕉,芭乐,柚子,李子,杨桃,猕猴桃,哈密瓜,葡萄,榴莲; 实现功能:提示用户输入水果品种编号,输出该水果的全部信息。

    class Lei { String name; String color; int price; int date; int num; String zz; void assemble(){ Sys ...

  6. 使用go写一个简单的exe文件

    工作需要一个小工具给分析师用,原先打算写一个脚本的,但是呢我又不会用python,要写的话只能用java来实现(打包成可执行jar,使用java -jar 的命令来执行,当然得安装jdk).这种命令行 ...

  7. Vue局部注册 或者全局注册 组件时,组件定义要用 分隔命名,用驼峰命名是不生效的

    Vue.component('all-canuse',{ props:['message'], template:'<div>{{message}}</div>' }) 像这样

  8. Reference Type Casting

    5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...

  9. 字符编码的来源,ascii、unicode和utf-8编码的关系

    字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...

  10. python-tornado-hello,world

    #!/usr/bin/python import tornado.httpserver import tornado.ioloop import tornado.options import torn ...