spring boot 内嵌数据库
来源:互联网 发布:下载放大镜软件 编辑:程序博客网 时间:2024/06/05 20:09
Mybatis + spring boot(内嵌数据库)
1. 创建一个maven工程mybatis-spring-boot:
2. 工程结构:
3. 添加以下依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boot.mybatis</groupId>
<artifactId>mybatis-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis-spring-boot</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- <dependency> -->
<!-- <groupId>com.zaxxer</groupId>-->
<!-- <artifactId>HikariCP</artifactId>-->
<!-- 版本号可以不用指定,SpringBoot会选用合适的版本 -->
<!-- </dependency> -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- testdependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>ognl</groupId>-->
<!-- <artifactId>ognl</artifactId>-->
<!-- <version>3.1.2</version>-->
<!-- <scope>test</scope> -->
<!-- <exclusions> -->
<!-- <exclusion> -->
<!-- <groupId>javassist</groupId>-->
<!-- <artifactId>javassist</artifactId>-->
<!-- </exclusion> -->
<!-- </exclusions> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.javassist</groupId>-->
<!-- <artifactId>javassist</artifactId>-->
<!-- <version>3.20.0-GA</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. application.properties 文件:
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#
#spring.datasource.url=jdbc:mysql://localhost:3306/haishi
#spring.datasource.username=root
#spring.datasource.password=haixie
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath*:com/mybatis/boot/mapper/*Mapper.xml
mybatis.type-aliases-package=com.mybatis.boot.domain
spring.datasource.schema=import.sql
#mybatis.config-location=mybatis-config.xml
logging.level.root=WARN
logging.level.sample.mybatis.mapper=TRACE
import.sql文件:
droptable ifexists city;
droptable ifexists hotel;
createtable city (idintprimarykeyauto_increment, namevarchar, statevarchar, countryvarchar);
createtable hotel(cityint, namevarchar, addressvarchar, zipvarchar);
insertinto city(name, state, country)values ('SanFrancisco','CA', 'US');
insertintohotel(city, name, address, zip)values (1,'ConradTreasury Place','William & George Streets','4001')
添加实体类City 和Hotel:
packagecom.mybatis.boot.domain;
importjava.io.Serializable;
publicclass CityimplementsSerializable {
privatestaticfinallongserialVersionUID = 1L;
private Longid;
private Stringname;
private Stringstate;
private Stringcountry;
public Long getId() {
returnthis.id;
}
publicvoid setId(Longid) {
this.id = id;
}
public String getName() {
returnthis.name;
}
publicvoidsetName(String name) {
this.name = name;
}
public String getState() {
returnthis.state;
}
publicvoidsetState(String state) {
this.state = state;
}
public String getCountry() {
returnthis.country;
}
publicvoidsetCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return getId() +"," +getName() +"," + getState() +"," +getCountry();
}
}
packagecom.mybatis.boot.domain;
import java.io.Serializable;
publicclass HotelimplementsSerializable {
privatestaticfinallongserialVersionUID = 1L;
private Longcity;
private Stringname;
private Stringaddress;
private Stringzip;
public Long getCity() {
returncity;
}
publicvoidsetCity(Long city) {
this.city = city;
}
public String getName() {
returnname;
}
publicvoidsetName(String name) {
this.name = name;
}
public String getAddress() {
returnaddress;
}
publicvoidsetAddress(String address) {
this.address = address;
}
public String getZip() {
returnzip;
}
publicvoidsetZip(String zip) {
this.zip = zip;
}
@Override
public String toString() {
return getCity()+"," + getName() +"," +getAddress() +"," + getZip();
}
}
添加dao:
packagecom.mybatis.boot.dao;
importorg.apache.ibatis.session.SqlSession;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
importcom.mybatis.boot.domain.City;
@Component
publicclass CityDao {
@Autowired
private SqlSessionsqlSession;
public City selectCityById(long id) {
returnthis.sqlSession.selectOne("selectCityById", id);
}
}
添加mapper:
packagecom.mybatis.boot.mapper;
importorg.apache.ibatis.annotations.Mapper;
importcom.mybatis.boot.domain.Hotel;
@Mapper
publicinterfaceHotelMapper {
Hotel selectByCityId(int city_id);
}
添加SampleXmlApplication (mian()方法启动服务器):
packagecom.mybatis.boot;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.CommandLineRunner;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importcom.mybatis.boot.dao.CityDao;
importcom.mybatis.boot.mapper.HotelMapper;
@SpringBootApplication
publicclassSampleXmlApplicationimplements CommandLineRunner {
@Autowired
private CityDaocityDao;
@Autowired
private HotelMapperhotelMapper;
publicstaticvoidmain(String[] args) {
SpringApplication.run(SampleXmlApplication.class, args);
}
@Override
publicvoidrun(String... args)throws Exception {
System.out.println(this.cityDao.selectCityById(1)+" 啊啊");
System.out.println(this.hotelMapper.selectByCityId(1)+" 是是");
}
}
添加CityMapper.xml和HotelMapper.xml:
<?xml version="1.0"encoding="UTF-8"?>
<!--
Copyright 2015-2016 the original authoror authors.
Licensed under the ApacheLicense, Version 2.0 (the "License");
you may not use this file except incompliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law oragreed to in writing, software
distributed under the License isdistributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
See the License for the specificlanguage governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="sample.mybatis.mapper.CityMapper">
<selectid="selectCityById"resultType="City">
select * from city where id = #{id}
</select>
</mapper>
和
<?xml version="1.0"encoding="UTF-8"?>
<!--
Copyright 2015-2016 the original authoror authors.
Licensed under the ApacheLicense, Version 2.0 (the "License");
you may not use this file except incompliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law oragreed to in writing, software
distributed under the License isdistributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
See the License for the specificlanguage governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.boot.mapper.HotelMapper">
<selectid="selectByCityId"resultType="Hotel">
select * from hotel where city = #{id}
</select>
</mapper>
- spring boot 内嵌数据库
- Spring Boot内嵌数据库的使用(H2)
- spring boot 启动web,内嵌tomcat
- Spring boot 不用内嵌的tomcate
- Spring Boot对内嵌数据库的支持
- [spring-boot] 配置数据库
- Spring Boot,找不到数据库?
- spring boot实战(第七篇)内嵌容器tomcat配置
- 选择Spring Boot项目的内嵌容器
- spring boot内嵌tomcat配置端口更改
- 选择Spring Boot项目的内嵌容器
- Spring Boot 内嵌Tomcat的端口号的修改
- spring boot实战(第七篇)内嵌容器tomcat配置
- spring boot jar包tomcat内嵌方式启动
- Spring Boot JDBC 连接数据库
- Spring Boot JPA 连接数据库
- Spring Boot MyBatis 连接数据库
- spring boot mybatis连接数据库
- html网页详细代码
- Jrebel 6 license server激活方法
- 垂直滚动公告(垂直跑马灯)
- Android 自定义View (三) 圆环交替 等待效果
- mac 安装tomcat8
- spring boot 内嵌数据库
- css让div在整个网页水平、垂直居中解决方法
- xcode官方版本离线下载
- SSIS用MDX 查询或多维数据库作为数据源报错的解决办法
- SAXParseException:cvc-elt.1:找不到元素“beans”的声明
- SpringMVC Json格式输出
- error while loading shared libraries: xxx.so.x"错误的原因和解决办法
- 手机站常用的基本css
- unity学习笔记~材质金属颜色对照表