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-052】052最新考试题库分析整理-第7题

    7.Which is true about external tables? A) The ORACLE_DATAPUMP access driver can be used to write dat ...

  2. 为解决Samba windows 无法访问 尝试过的方法

    1, 通过   vi /etc/sysconfig/selinux 把 SELINUX=enforcing   修改为SELINUX= disable 退出保存,并且重启.(设置了) 2, 把wind ...

  3. [ActionScript 3.0] File下载工具

    更新数据原理,访问接口,将服务器数据抓取并下载到本地的临时文件夹,当所有下载完成,卸载客户端内容,出现升级界面,此时移动下载的内容到目标文件夹,移动完成再重新加载客户端,访问接口,下载文件,移动文件均 ...

  4. 【FAQ】Maven 本地仓库明明有jar包,pom文件还是报错解决办法

    方法一: 找到出错的jar包文件位置,删掉_maven.repositories文件 方法二: maven中的本地仓库的index索引没有更新导致 解决方案: 在eclipse中打开菜单 window ...

  5. [转] YUM 源优先级插件:Yum Priorities

    Linux 发行版比较多,同时还有很多个人或组织维护了某些特定用途的安装/升级源.Yum Priorities 插件可以用来强制保护源.它通过给各个源设定不同的优先级,使得系统管理员可以将某些源(比如 ...

  6. Java性能优化技巧及实战

    关于Java代码的性能优化,是每个javaer都渴望掌握的本领,进而晋升为大牛的必经之路,但是对java的调优需要了解整个java的运行机制及底层调用细节,需要多看多读多写多试,并非一朝一夕之功.本文 ...

  7. php pdo prepare真的安全吗

    详见 这里 Let's say I have code like this: $dbh = new PDO("blahblah"); $stmt = $dbh->prepar ...

  8. 题目1002:Grading(简单判断)

    问题来源 http://ac.jobdu.com/problem.php?pid=1002 问题描述 题目背景为高考试卷批改打分制度.对于每一道题,至少需要两位评审老师进行打分, 当两个老师的打分结果 ...

  9. C# openfiledialog的使用

    文件对话框(FileDialog) 一.打开文件对话框(OpenFileDialog) 1. OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录Filt ...

  10. WebDriver+TestNG的一个典型例子

    想让测试更加灵活,1. 可以配置使用任意支持的浏览器进行测试:2. 配置所有Google的URL:3. 配置搜索的关键字.修改后的代码: public class GoogleTest { WebDr ...