spring boot 内嵌数据库

 

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 if exists city;

droptable if exists hotel;

 

createtable city (id intprimarykeyauto_increment, name varchar, state varchar, country varchar);

createtable hotel(city int, name varchar, address varchar, zip varchar);

 

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 City implementsSerializable {

 

    privatestaticfinallongserialVersionUID = 1L;

 

    private Long id;

 

    private String name;

 

    private String state;

 

    private String country;

 

    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 Hotel implementsSerializable {

 

    privatestaticfinallongserialVersionUID = 1L;

 

    private Long city;

 

    private String name;

 

    private String address;

 

    private String zip;

 

    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;

import org.springframework.stereotype.Component;

 

importcom.mybatis.boot.domain.City;

 

@Component

publicclass CityDao {

 

    @Autowired

    private SqlSession sqlSession;

 

    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

publicclassSampleXmlApplication implements CommandLineRunner {

 

    @Autowired

    private CityDao cityDao;

 

    @Autowired

    private HotelMapper hotelMapper;

   

    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">

    <select id="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">

    <select id="selectByCityId" resultType="Hotel">

        select * from hotel where city = #{id}

    </select>

</mapper>

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值