SpringBoot + MyBatis + H2

       在SpringBoot环境下使用Mybatis,数据库使用内嵌数据库H2

环境

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>zl</groupId>
	<artifactId>mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>mybatis</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
<!-- 		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency> -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

properties

server.port=8080

#************H2  Begin****************
#db schema
spring.datasource.schema=classpath:db/schema.sql
#db data
spring.datasource.data=classpath:db/data.sql

#remote visit
spring.h2.console.settings.web-allow-others=true
#console url
spring.h2.console.path=/h2-console
#default true
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true

#db url,default :jdbc:h2:mem:testdbsa
spring.datasource.url=jdbc:h2:mem:testdbsa
#driver default:org.h2.Driver
spring.datasource.driver-class-name=org.h2.Driver
#default sa
spring.datasource.username=sa
#default null
spring.datasource.password=
#************H2  End****************

#************Mybatis  Begin****************
mybatis.config-location=classpath:mybatis-config.xml
#************Mybatis  End****************

#************Log  Begin****************
logging.level.root=WARN
#mapper log
logging.level.mybatis.mapper=TRACE
#view initialize message
logging.level.org.hibernate=DEBUG
#************Log  Begin****************



H2

     H2内嵌数据库,如properties配置,开启console控制,启动logging.level.org.hibernate方便查看初始化H2信息(需要jpa的依赖).

      启动Spring, 访问http://localhost:8080/h2-console/,即出现控制


连接


     有两个是通过程序插入,其他consle使用详见官网


mybatis

      springboot整合mybatis参考

基本目录结构


以City为例

CityMapper

package mybatis.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import mybatis.model.City;

@Mapper
public interface CityMapper {

	/**
	 *注解
	 */
	@Select("select * from city where state = #{state}")
	City findByState(@Param("state") String state);
	
	/**xml 
	 */
	City selectCityById(int city_id);

}

     基于两种,一个注解,一种使用xml,注解的局限性太大,一般使用xml配置,xml配置需额外配置(mybatis-config.xml,CityMapper.xml),对于@Mapper注解可在application中进行统一注解

@SpringBootApplication
@MapperScan("mybatis.mapper")
public class MybatisApplication {
application.properties指定mybatis配置文件

mybatis.config-location=classpath:mybatis-config.xml
mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="mybatis.model"/>
    </typeAliases>
    <mappers>
        <mapper resource="mapper/CityMapper.xml"/>
        <mapper resource="mapper/HotelMapper.xml"/>
    </mappers>
</configuration>

      对上面mappers,可在application.properties中统一配置

mybatis.mapper-locations=classpath:mapper/*.xml
CityMapper.xml

<?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="mybatis.mapper.CityMapper">
    <select id="selectCityById" resultType="City">
        select * from city where id = #{id}
    </select>
</mapper>

City

package mybatis.model;

import java.io.Serializable;

public class City implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;

	private String name;

	private String state;

	private String country;

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getState() {
		return this.state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountry() {
		return this.country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return getId() + "," + getName() + "," + getState() + "," + getCountry();
	}
	
}

data.sql

insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
insert into hotel(city, name, address, zip) values (1, 'Conrad Treasury Place', 'William & George Streets', '4001')
schema.sql
drop table if exists city;
drop table if exists hotel;

create table city (id int primary key auto_increment, name varchar, state varchar, country varchar);
create table hotel (city int, name varchar, address varchar, zip varchar);

启动

package mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import mybatis.mapper.CityMapper;
import mybatis.mapper.HotelMapper;

@SpringBootApplication
public class MybatisApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext run = SpringApplication.run(MybatisApplication.class, args);
		CityMapper cityMapper = run.getBean(CityMapper.class);
		System.out.println(cityMapper.findByState("CA"));
		System.out.println(cityMapper.selectCityById(1));
		
		HotelMapper hotelMapper = run.getBean(HotelMapper.class);
		System.out.println(hotelMapper.selectByCityId(1));
	}
}

DataSource

     对于只有一个数据源,使用SpringBoot自动装配的数据源,显得更为优雅,但更多时候需要自定义数据源(比如定义多个数据源)或保证数据源与SpringBoot解耦合(某些SpringBoot并未集成数据源格式).

package mybatis.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages="mybatis.mapper",sqlSessionFactoryRef="sqlSessionFactory")
public class MybatisDataSourceConfig {

	//此处使用自动装配的H2数据源,可自定义数据源
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        //sessionFactory.setTypeAliasesPackage("mybatis.model");
        sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return sessionFactory.getObject();
    }
}
          此处仅使用自动装配的h2 dataSource,其余mapper相关配置均独立配置,因此application.properties中的相关配置需注释

#************Mybatis  Begin****************
#mybatis.config-location=classpath:mybatis-config.xml
#mybatis.mapper-locations=classpath:mapper/*.xml
#************Mybatis  End****************
          自定义装配数据源,需禁用SpringBoot的自动装配,同时保证每个每个mybatis factory的独立性,其mapper 相关设置均独立设置,因此将@MapperScan移至此config下,application改动

//DataSourceAutoConfiguration 自动配置spring.datasource.*数据源(此处加上,还是会自动生成DataSource???)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

     MybatisDataSourceConfig配置自定义事物(SpringBoot自动装配了事物DataSourceTransactionManager)

    @Bean
    @Primary
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
    	return new DataSourceTransactionManager(dataSource);
    }
     同时也需要@EnableTransactionManagement开启注解(SpringBoot 其他autoConfig也配置了,可能会复用,此处不配也能生效)

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的案例,用于演示如何使用Spring Boot和MyBatis来构建后端REST API,并使用Vue.js来构建前端界面。 首先,我们需要创建一个Spring Boot项目。您可以使用Spring Initializr来快速创建一个新项目。选择Maven项目,然后添加所需的依赖项:Spring Web、MyBatis、MySQL驱动程序和Lombok。 接下来,我们需要创建一个数据模型。假设我们正在构建一个简单的博客系统,我们需要一个名为“Post”的实体类: ```java @Data @NoArgsConstructor @AllArgsConstructor public class Post { private Long id; private String title; private String content; private Date createTime; private Date updateTime; } ``` 接下来,我们需要创建一个MyBatis Mapper接口来执行CRUD操作。我们将在此接口中定义SQL查询: ```java @Mapper public interface PostMapper { @Select("SELECT * FROM post") List<Post> findAll(); @Select("SELECT * FROM post WHERE id=#{id}") Post findById(Long id); @Insert("INSERT INTO post(title,content,create_time,update_time) VALUES(#{title},#{content},#{createTime},#{updateTime})") @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") void insert(Post post); @Update("UPDATE post SET title=#{title},content=#{content},update_time=#{updateTime} WHERE id=#{id}") void update(Post post); @Delete("DELETE FROM post WHERE id=#{id}") void delete(Long id); } ``` 然后,我们需要创建一个PostService类来将Mapper方法暴露为REST API: ```java @Service public class PostService { @Autowired private PostMapper postMapper; public List<Post> findAll() { return postMapper.findAll(); } public Post findById(Long id) { return postMapper.findById(id); } public void insert(Post post) { Date now = new Date(); post.setCreateTime(now); post.setUpdateTime(now); postMapper.insert(post); } public void update(Post post) { post.setUpdateTime(new Date()); postMapper.update(post); } public void delete(Long id) { postMapper.delete(id); } } ``` 最后,我们需要创建一个REST控制器来处理HTTP请求: ```java @RestController @RequestMapping("/api/posts") public class PostController { @Autowired private PostService postService; @GetMapping("/") public List<Post> findAll() { return postService.findAll(); } @GetMapping("/{id}") public Post findById(@PathVariable Long id) { return postService.findById(id); } @PostMapping("/") public void insert(@RequestBody Post post) { postService.insert(post); } @PutMapping("/{id}") public void update(@RequestBody Post post, @PathVariable Long id) { post.setId(id); postService.update(post); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { postService.delete(id); } } ``` 现在,我们可以启动应用程序并使用Postman或类似的工具来测试我们的REST API。 最后,我们需要创建一个Vue.js前端界面。您可以使用Vue CLI来快速创建一个新项目,并使用axios库来调用我们的REST API: ```html <template> <div> <h1>My Blog Posts</h1> <table> <thead> <tr> <th>ID</th> <th>Title</th> <th>Content</th> <th>CreateTime</th> <th>UpdateTime</th> <th>Actions</th> </tr> </thead> <tbody> <tr v-for="post in posts" :key="post.id"> <td>{{post.id}}</td> <td>{{post.title}}</td> <td>{{post.content}}</td> <td>{{post.createTime}}</td> <td>{{post.updateTime}}</td> <td><button @click="editPost(post)">Edit</button> <button @click="deletePost(post)">Delete</button></td> </tr> </tbody> </table> <br> <h2 v-if="!editing">Add Post</h2> <h2 v-if="editing">Edit Post</h2> <form @submit.prevent="submitPost"> <label>Title:</label><br> <input type="text" v-model="post.title"><br> <label>Content:</label><br> <textarea v-model="post.content"></textarea><br> <button v-if="!editing">Add</button> <button v-if="editing">Save</button> </form> </div> </template> <script> import axios from 'axios'; export default { name: 'App', data() { return { posts: [], post: { title: '', content: '' }, editing: false } }, mounted() { axios.get('/api/posts').then(response => { this.posts = response.data; }); }, methods: { submitPost() { if (this.editing) { axios.put('/api/posts/' + this.post.id, this.post).then(response => { this.editing = false; this.post = { title: '', content: '' }; axios.get('/api/posts').then(response => { this.posts = response.data; }); }); } else { axios.post('/api/posts', this.post).then(response => { this.post = { title: '', content: '' }; axios.get('/api/posts').then(response => { this.posts = response.data; }); }); } }, editPost(post) { this.editing = true; this.post = { id: post.id, title: post.title, content: post.content }; }, deletePost(post) { axios.delete('/api/posts/' + post.id).then(response => { axios.get('/api/posts').then(response => { this.posts = response.data; }); }); } } } </script> ``` 这就是一个简单的案例,使用Spring Boot、MyBatis和Vue.js构建了一个完整的Web应用程序。您可以根据需要进行更改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值