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>

 

 

 

 

0 0
原创粉丝点击