Springboot整合mybatis

一、使用工具:Eclipse     mysql8.0     maven

二、设计数据库

CREATE TABLE `product`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `price` double NOT NULL,
  `num` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;


INSERT INTO `product` VALUES (1, '电冰箱', 3600, 51);
INSERT INTO `product` VALUES (2, '电视机', 2300, 30);
INSERT INTO `product` VALUES (3, '电灯泡', 30, 60);
INSERT INTO `product` VALUES (4, '笔记本', 7600, 20);

三、项目目录结构:

四、建立maven项目,在pom.xml文件中配置

  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>  
    <!-- 配置开发模式,代码修改了不用重新运行 -->
  	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>springloaded</artifactId>
	    <version>1.2.8.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-devtools</artifactId>
	    <version>2.1.6.RELEASE</version>
	</dependency>
    <!-- mybatis依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- druid依赖 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!-- 分页插件(没使用到) -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.10</version>
    </dependency>
  </dependencies>
    
  <build>
   <plugins>
      <!-- maven项目packing为war类型时,可以不配置WEB-INFO,但必须加上该插件 -->
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <version>2.3</version>
         <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
         </configuration>
      </plugin>
   </plugins>
  </build>

五、配置文件(mybatis-config.xml,application.yml,ProductMapper.xml)

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

application.yml:

#数据源配置
spring:
  datasource:
    name: springboot
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMissis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

#mybatis配置
mybatis:
  mapper-locations: classpath:mapping/ProductMapper.xml
  type-aliases-package: com.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  
#分页插件	
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

ProductMapper.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.mapper.ProductMapper">
	<insert id="addProduct" parameterType="product">
		insert into product(id,name,price,num) values(#{id},#{name},#{price},#{num})
	</insert>
</mapper>

六、写启动类(注意扫描包,扫描包和扫描别的层面用的不是一个注解)

package com.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"com.controller","com.service","com.pojo"})
@MapperScan("com.mapper")
public class SpringApplications {

	public static void main(String[] args) {
		SpringApplication.run(SpringApplications.class, args);
	}

}

七、层次结构

Product.java

package com.pojo;

public class Product {

	private int id;
	private String name;
	private Double price;
	private int num;

    构造方法(省略)	
	getter/setter方法(省略)
	
}

ProductMapper.java

package com.mapper;

import com.pojo.Product;

public interface ProductMapper {
	void addProduct(Product product);
}

ProductService.java

package com.service;

import com.pojo.Product;

public interface ProductService {
	public void save(Product p);
}

ProductServiceImpl.java

package com.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.ProductMapper;
import com.pojo.Product;
import com.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Resource
	private  ProductMapper productMapper;
	
	@Override
	public void save(Product p) {
		productMapper.addProduct(p);
	}

}

ProductController.java

package com.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Product;
import com.service.ProductService;

@Controller
public class ProductController {

	@Resource
	private ProductService productService;
	
	@RequestMapping("/saveProduct")
	@ResponseBody
	public String save(){
		Product product = new Product();
		product.setId(21);
		product.setName("保温杯");
		product.setPrice(56.4);
		product.setNum(100);
		productService.save(product);
		return "success";
	}
	
}

八、运行此程序,运行结束,完美插入一条数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值