SpringBoot中使用H2作为内存数据库

当前环境:JDK1.8、MAVEN 3.6.1、eclipse

1.导入h2的依赖

当前pom文件的内容:

   <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.4.RELEASE</version>
		<relativePath />
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 如果添加了springBoot中隊jpa的依賴,就需要在application.properties中设置数据源 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<!-- 使用h2内存数据库 -->
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.为当前的h2数据库建立DataSource

当前的application.properties的内容:
server.port=9090
server.context-path=/springboot-h2db
#h2 database properties
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1#使用当前的内存数据库
spring.datasource.username=sa
spring.datasource.password=

3.编写实体类

当前实体类User的内容:

@Entity
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	private String name;

	private Date birth;

	@Column(name = "on_work")
	private Boolean onWork;// 还是原来的问题
	....省略getter、setter和toString方法

4.日期转换器和dao层

日期转换器省略
当前的dao

/**
 * @description User的dao层
 * @author hy
 * @date 2019-08-12
 */
@Repository
public interface UserDao extends JpaRepository<User, Integer> {
}

5.编写service和service的实现层

UserService中的内容如下

/**
 * @description userservice接口
 * @author hy
 * @date 2019-08-12
 */
public interface UserService {
	List<User> findAllUser();

	User findUserById(Integer id);

	void updateUser(User user);

	void addUser(User user);

	void deleteUserById(Integer id);
}

当前的UserServiceImpl的内容为:

/**
 * @description userService的实现类
 * @author hy
 * @date 2019-08-12
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;

	/**
	 * @description 查询所有的用户信息
	 */
	@Override
	public List<User> findAllUser() {
		return userDao.findAll();
	}

	/**
	 * @description 根據id编号查询用户信息
	 */
	@Override
	public User findUserById(Integer id) {
		return userDao.findOne(id);
	}

	/**
	 * @description 更新用户信息
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void updateUser(User user) {
		User one = userDao.getOne(user.getId());
		if (one != null) {
			one.setBirth(user.getBirth());
			one.setName(user.getName());
			one.setOnWork(user.getOnWork());
			System.out.println("更新成功!");
			return;
		}
		System.out.println("更新失败");

	}

	/**
	 * @description 添加用户信息
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void addUser(User user) {
		userDao.save(user);
	}

	/**
	 * @description 根据id删除数据
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void deleteUserById(Integer id) {
		userDao.delete(id);
	}

}

6.创建controller层

其中的UserController中内容:

/**
 * @description user的controller层
 * @author hy
 * @date 2019-08-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping("/userList")
	public String userList() {
		List<User> findAllUser = userService.findAllUser();
		return findAllUser.toString();
	}

	@RequestMapping("/findUserById/{id}")
	public String findUserById(@PathVariable("id") Integer id) {
		User findUserById = userService.findUserById(id);
		return findUserById.toString();
	}

	@RequestMapping("/updateUser")
	public String updateUser(User user) {
		userService.updateUser(user);
		return "【更新 " + user.getId() + " 成功】";
	}

	@RequestMapping("/addUser")
	public String addUser(User user) {
		userService.addUser(user);
		return "【添加 " + user.getName() + " 用戶成功】";
	}

	@RequestMapping("/deleteUserById/{id}")
	public String deleteUserById(@PathVariable("id") Integer id) {
		userService.deleteUserById(id);
		return "【刪除编号为 " + id + " 成功】";
	}
}

7.创建入口类

当前的Application类中的内容如下:

/**
 * @description 使用SpringBoot操作h2内存数据库
 * @author hy
 * @date 2019-08-12
 */
@RestController
@SpringBootApplication
public class Application {
	@Autowired
	DataSource dataSource;

	/**
	 * @description 数据源的连接是否正常
	 * @throws SQLException
	 */
	@RequestMapping("/dataSource")
	public void dataSource() throws SQLException {
		dataSource.getConnection().toString();
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

8.创建测试

各个测试都正常!当前的操作h2数据库完成!

9.总结

1.使用SpringBoot操作h2数据的时候,需要导入依赖,并指定当前的使用方式为内存数据库:mem

2.需要创建当前h2的dataSource

  • spring.datasource.driver-class-name=org.h2.Driver
  • spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1
  • spring.datasource.username=sa
  • spring.datasource.password=

3.编写实体类,并添加标记@Entity,以及@Id和@GeneratedValue(strategy = GenerationType.AUTO)

4.编写dao层标记@Reposity继承jpa的Reposity等接口来简化操作

5.编写controller层,然后测试

以上纯属个人见解,如有问题请联系本人!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值