配置maven
maven依赖报错
@echo off
rem create by sunhao(sunhao.java@gmail.com)
rem crazy coder
set REPOSITORY_PATH=D:\m2
rem 正在搜索…
for /f “delims=” %%i in (‘dir /b /s “%REPOSITORY_PATH%*lastUpdated*”’) do (
del /s /q %%i
)
rem 搜索完毕
pause
1.导包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<!--springboot的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.入口类
package org.my;
import org.springframework.boot.SpringApplication;
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
// 注意:参数是入口类的类对象,而不是SpringApplication
SpringApplication.run(SpringBootApplication.class,args);
}
}
3.application.yaml
server:
# 注意:严格区分tab和空格
port: 8989
context-path: /springboot-demo
4.测试
package org.my.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("test")
public class Test {
@RequestMapping("test")
@ResponseBody
public String sayHello(){
return "sayHello";
}
}
5.集成mybatis
准备工作:测试数据库连接是否正常
package org.my.conn;
import org.junit.Test;
import java.sql.DriverManager;
public class Conn {
@Test
public void conn() throws Exception {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println(DriverManager.getConnection("jdbc:mysql://localhost:3306/novel","root","root"));
}
}
# 集成mybatis的mysql配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/novel
username: root
password: root
5.1导jar
<!--和mybatis集成所需要的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--alibaba的druiDataSource-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--mybatis和springboot集成的启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
5.2 建表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for province
-- ----------------------------
DROP TABLE IF EXISTS `province`;
CREATE TABLE `province` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(6) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of province
-- ----------------------------
INSERT INTO `province` VALUES ('1', '110000', '北京市');
INSERT INTO `province` VALUES ('2', '120000', '天津市');
INSERT INTO `province` VALUES ('3', '130000', '河北省');
5.3实体类
准备:安装lombok插件:实体类的注解需要
①导jar
package org.my.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
//提供类所有属性的getting和setting方法,此外还提供了equals、canEqual、hashCode、toString方法
@Data
// 无参构造
@NoArgsConstructor
// 有参构造
@AllArgsConstructor
// 支持链式调用
@Accessors(chain=true)
public class Province {
private Integer id;
private String code;
private String name;
}
② mapper包
package org.my.mapper;
import org.my.entity.Province;
import java.util.List;
public interface ProvinceMapper {
List<Province> selectAllProvinces();
}
③mapper文件
<?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.my.mapper.ProvinceMapper">
<cache/>
<select id="selectAllProvinces" resultType="Province">
select * from province
</select>
</mapper>
5.4 service和serviceImpl
// service
package org.my.service;
import org.my.entity.Province;
import java.util.List;
public interface ProvinceService {
List<Province> selectAllProvinces();
}
// serviceImpl
package org.my.service;
import org.my.entity.Province;
import org.my.mapper.ProvinceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ProvinceServiceImpl implements ProvinceService {
@Autowired
ProvinceMapper provinceMapper;
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<Province> selectAllProvinces() {
List<Province> provinceList = provinceMapper.selectAllProvinces();
return provinceList;
}
}
5.5入口类加上mapper包的扫描
// 扫描mapper包
@MapperScan("org.my.mapper")
5.6 给mapper文件注册和实体类起别名
mybatis:
type-aliases-package: org.my.entity
mapper-locations: org/my/mapper/*.xml
5.7 测试
package org.my.controller;
import org.my.entity.Province;
import org.my.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("test")
public class Test {
@Autowired
ProvinceService provinceService;
@RequestMapping("testSelectAllProvinces")
@ResponseBody
public List<Province> testSelectAllProvinces(){
List<Province> provinceList = provinceService.selectAllProvinces();
return provinceList;
}
}
// 访问路径 http://localhost:8989/springboot-demo/test/testSelectAllProvinces