springBoot 的 整合(junit ,mybatis)
1.... pom.xml 依赖jar包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc</groupId> 8 <artifactId>springBoot01</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <!--继承SpringBoot父类 spring-boot-starter-parent--> 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.0.2.RELEASE</version> 15 </parent> 16 17 <properties> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <maven.compiler.source>1.8</maven.compiler.source> 20 <maven.compiler.target>1.8</maven.compiler.target> 21 </properties> 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <!--添加起步依赖--> 28 <dependency> 29 <groupId>mysql</groupId> 30 <artifactId>mysql-connector-java</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-freemarker</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-data-jpa</artifactId> 39 </dependency> 40 <!--mybatis 起步依赖--> 41 <dependency> 42 <groupId>org.mybatis.spring.boot</groupId> 43 <artifactId>mybatis-spring-boot-starter</artifactId> 44 <version>1.1.1</version> 45 </dependency> 46 <!--测试的起步依赖--> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 <scope>test</scope> 51 </dependency> 52 </dependencies> 53 <!-- build 作用 : 扫描 mapper 下的 FlightMapper类 完成 自动注入对象, 54 Invalid bound statement (not found): com.wsc.core.mapper.FlightMapper.getFlight --> 55 <build> 56 <resources> 57 <resource> 58 <directory>src/main/java</directory> 59 <includes> 60 <include>**/*.properties</include> 61 <include>**/*.xml</include> 62 </includes> 63 <filtering>false</filtering> 64 </resource> 65 <resource> 66 <directory>src/main/resources</directory> 67 <includes> 68 <include>**/*.*</include> 69 </includes> 70 <filtering>false</filtering> 71 </resource> 72 </resources> 73 </build> 74 </project>
2.....yaml格式 数据库连接 resources / application.yml 封装的 数据源信息 yaml格式数据
application.yml
3....mapper / FlightMapper 类 ,FlightMapper.xml mybatis 的映射文件 SQL语句
1 package com.wsc.core.mapper; 2 3 import com.wsc.core.entity.Flighties; 4 5 import java.util.List; 6 7 public interface FlightMapper { 8 public List<Flighties> getFlight(); 9 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!--namespace 命名空间 类全名--> 6 <mapper namespace="com.wsc.core.mapper.FlightMapper"> 7 <select id="getFlight" resultType="com.wsc.core.entity.Flighties"> 8 select * from flight 9 </select> 10 </mapper>
4....实体类
1 package com.wsc.core.entity; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 /** 7 * @version 1.0 8 * @ClassName Person 9 * @Description TODO 10 * @Author WSC 11 * @Date 2019/8/23 11:11 12 **/ 13 @ConfigurationProperties(prefix = "person") // 把属性的值批量绑定一个对象上 14 @Component 15 public class Person { 16 private String name; 17 private int age; 18 private String sex; 19 private String address; 20 private String[] myAddress; 21 private String[] myAddress1; 22 public String getName() { 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getAge() { 31 return age; 32 } 33 34 public void setAge(int age) { 35 this.age = age; 36 } 37 38 public String getSex() { 39 return sex; 40 } 41 42 public void setSex(String sex) { 43 this.sex = sex; 44 } 45 46 public String getAddress() { 47 return address; 48 } 49 50 public void setAddress(String address) { 51 this.address = address; 52 } 53 54 public String[] getMyAddress() { 55 return myAddress; 56 } 57 58 public String[] getMyAddress1() { 59 return myAddress1; 60 } 61 62 public void setMyAddress1(String[] myAddress1) { 63 this.myAddress1 = myAddress1; 64 } 65 66 public void setMyAddress(String[] myAddress) { 67 this.myAddress = myAddress; 68 } 69 }
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 import java.io.Serializable; 5 import java.util.Date; 6 7 /** 8 * @version 1.0 9 * @ClassName Flight 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/22 14:35 13 **/ 14 15 public class Flighties implements Serializable { 16 17 private Integer flight_id; 18 private String flight_no; 19 private String departure_city; 20 private Date departure_time; 21 private Date arrival_time; 22 private String arrival_city; 23 24 public Integer getFlight_id() { 25 return flight_id; 26 } 27 28 public void setFlight_id(Integer flight_id) { 29 this.flight_id = flight_id; 30 } 31 32 public String getFlight_no() { 33 return flight_no; 34 } 35 36 public void setFlight_no(String flight_no) { 37 this.flight_no = flight_no; 38 } 39 40 public String getDeparture_city() { 41 return departure_city; 42 } 43 44 public void setDeparture_city(String departure_city) { 45 this.departure_city = departure_city; 46 } 47 48 public Date getDeparture_time() { 49 return departure_time; 50 } 51 52 public void setDeparture_time(Date departure_time) { 53 this.departure_time = departure_time; 54 } 55 56 public Date getArrival_time() { 57 return arrival_time; 58 } 59 60 public void setArrival_time(Date arrival_time) { 61 this.arrival_time = arrival_time; 62 } 63 64 public String getArrival_city() { 65 return arrival_city; 66 } 67 68 public void setArrival_city(String arrival_city) { 69 this.arrival_city = arrival_city; 70 } 71 72 @Override 73 public String toString() { 74 return "Flighties{" + 75 "flight_id=" + flight_id + 76 ", flight_no='" + flight_no + '\'' + 77 ", departure_city='" + departure_city + '\'' + 78 ", departure_time=" + departure_time + 79 ", arrival_time=" + arrival_time + 80 ", arrival_city='" + arrival_city + '\'' + 81 '}'; 82 } 83 }
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 import java.util.Date; 5 6 /** 7 * @version 1.0 8 * @ClassName Flight 9 * @Description TODO 10 * @Author WSC 11 * @Date 2019/8/22 14:35 12 **/ 13 @Entity //实体构造 14 @Table(name="flight") //表名 15 public class Flight { 16 @Id 17 @GeneratedValue(strategy = GenerationType.IDENTITY) //自增设置 18 /** 19 * @Column 注解(表的字段) 必须加上 ,不然 test 会报错 20 * 报错信息 : 数据库表 不存在 某个字段 21 * com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.ac_flight' doesn't exist 22 */ 23 @Column(name="flight_id") 24 private Integer id; 25 @Column(name="flight_no") 26 private String flightNo; 27 @Column(name="departure_city") 28 private String departureCity; 29 @Column(name="departure_time") 30 private Date departureTime; 31 @Column(name="arrival_time") 32 private Date arrivalTime; 33 @Column(name="arrival_city") 34 private String arrivalCity; 35 36 public Integer getId() { 37 return id; 38 } 39 40 public void setId(Integer id) { 41 this.id = id; 42 } 43 44 public String getFlightNo() { 45 return flightNo; 46 } 47 48 public void setFlightNo(String flightNo) { 49 this.flightNo = flightNo; 50 } 51 52 public String getDepartureCity() { 53 return departureCity; 54 } 55 56 public void setDepartureCity(String departureCity) { 57 this.departureCity = departureCity; 58 } 59 60 public Date getDepartureTime() { 61 return departureTime; 62 } 63 64 public void setDepartureTime(Date departureTime) { 65 this.departureTime = departureTime; 66 } 67 68 public Date getArrivalTime() { 69 return arrivalTime; 70 } 71 72 public void setArrivalTime(Date arrivalTime) { 73 this.arrivalTime = arrivalTime; 74 } 75 76 public String getArrivalCity() { 77 return arrivalCity; 78 } 79 80 public void setArrivalCity(String arrivalCity) { 81 this.arrivalCity = arrivalCity; 82 } 83 84 @Override 85 public String toString() { 86 return "Flight{" + 87 "id=" + id + 88 ", flightNo='" + flightNo + '\'' + 89 ", departureCity='" + departureCity + '\'' + 90 ", departureTime=" + departureTime + 91 ", arrivalTime=" + arrivalTime + 92 ", arrivalCity='" + arrivalCity + '\'' + 93 '}'; 94 } 95 }
5....service 修改数据 方法
1 package com.wsc.core.service; 2 3 import com.wsc.core.dao.FlightDao; 4 import com.wsc.core.entity.Flight; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import java.util.Optional; 10 11 /** 12 * @version 1.0 13 * @ClassName FlightService 14 * @Description TODO 15 * @Author WSC 16 * @Date 2019/8/23 13:24 17 **/ 18 @Service 19 public class FlightService { 20 @Autowired 21 private FlightDao flightDao; 22 @Transactional //事务 成功 更改 失败 不更改 23 //修改 数据 24 public void updateArrivalCity(){ 25 Optional<Flight> byId = flightDao.findById(1); 26 Flight flight = byId.get(); 27 flight.setArrivalCity("上海+郑州"); 28 flightDao.save(flight); 29 } 30 31 }
6....dao 继承JpaRepository接口
1 package com.wsc.core.dao; 2 3 import com.wsc.core.entity.Flight; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 // 继承JpaRepository 6 public interface FlightDao extends JpaRepository<Flight,Integer> { 7 }
7....controller 数据地址
1 package com.wsc.core.controller; 2 3 import com.wsc.core.dao.FlightDao; 4 import com.wsc.core.entity.Flight; 5 import com.wsc.core.entity.Flighties; 6 import com.wsc.core.entity.Person; 7 import com.wsc.core.mapper.FlightMapper; 8 import com.wsc.core.service.FlightService; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RestController; 12 13 import java.util.List; 14 15 /** 16 * @version 1.0 17 * @ClassName FlightController 18 * @Description TODO 19 * @Author WSC 20 * @Date 2019/8/22 14:58 21 **/ 22 @RestController //返回json数据 的格式 23 public class FlightController { 24 @Autowired 25 private FlightDao flightDao; 26 @Autowired 27 private Person person; 28 @Autowired 29 private FlightMapper flightMapper; 30 @Autowired 31 private FlightService flightService; 32 @RequestMapping("/flight/findAll") //访问地址 33 //全部数据 查询 34 public List<Flight> findAll(){ 35 List<Flight> flightList = flightDao.findAll(); 36 return flightList; 37 } 38 //获取yml文件的数据 person 39 @RequestMapping("/person") 40 public Person showPerson(){ 41 return person; 42 } 43 //获取flight数据库数据 mybatis 44 @RequestMapping("/flight/list2") 45 public List<Flighties> getList(){ 46 List<Flighties> flight = flightMapper.getFlight(); 47 return flight; 48 } 49 //修改数据 50 @RequestMapping("/flight/update") 51 public String updateArrivalCity(){ 52 flightService.updateArrivalCity(); 53 return "success"; 54 } 55 }
1 package com.wsc.core.controller; 2 3 import com.wsc.core.dao.FlightDao; 4 import com.wsc.core.entity.Flight; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Value; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.ResponseBody; 11 12 import java.util.HashMap; 13 import java.util.List; 14 import java.util.Map; 15 16 /** 17 * @version 1.0 18 * @ClassName ShowController 19 * @Description TODO 20 * @Author WSC 21 * @Date 2019/8/22 16:24 22 **/ 23 @Controller 24 public class ShowController { 25 @Autowired 26 private FlightDao flightDao; 27 @Value("${page.rows}") 28 private Integer rows; 29 @RequestMapping("/flight/data") //访问地址 30 //返回页面数据 31 public String showData(Model model){ 32 List<Flight> flightList = flightDao.findAll(); 33 model.addAttribute("flightList", flightList); 34 return "flight"; 35 } 36 37 /** 38 * 获取yaml里的 page 数据 39 * @Value("${page.rows}") private Integer rows; 40 * @return 41 */ 42 @RequestMapping("/page/rows") 43 @ResponseBody 44 public Map testYml(){ 45 Map map = new HashMap(); 46 map.put("rows",rows); 47 return map; 48 } 49 }
8....测试类 springBoot与Junit 整合 本地test类
1 package com.wsc.core; 2 3 import com.wsc.core.entity.Flighties; 4 import com.wsc.core.mapper.FlightMapper; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.boot.test.context.SpringBootTest; 9 import org.springframework.test.context.junit4.SpringRunner; 10 11 import java.util.List; 12 13 /** 14 * @version 1.0 15 * @ClassName testSpringBoot 16 * @Description TODO 17 * @Author WSC 18 * @Date 2019/8/23 14:06 19 **/ 20 @RunWith(SpringRunner.class) 21 @SpringBootTest(classes = Start.class) //加载启动 22 public class testSpringBoot { 23 @Autowired 24 private FlightMapper flightMapper; 25 @Test 26 public void testSpringBoot(){ 27 List<Flighties> flight = flightMapper.getFlight(); 28 for(Flighties flighties:flight){ 29 System.out.println(flighties); 30 } 31 32 } 33 }
9....启动类:@SpringBootApplication @MapperScan
1 package com.wsc.core; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 /** 8 * @version 1.0 9 * @ClassName Start 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/22 14:56 13 **/ 14 15 /** 16 * springBoot启动类 17 */ 18 @SpringBootApplication 19 @MapperScan(basePackages = "com.wsc.core.mapper") //扫描 mapper下的 所有 注解对象 20 public class Start { 21 /** 22 * springBoot 启动方法 23 * @param args 24 */ 25 public static void main(String[] args) { 26 //类名.class 27 SpringApplication.run(Start.class,args); 28 } 29 }
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!wished for you successed !!!