SpringBoot笔记3---如何读写数据库之使用CrudRepository中提供的方法来读写数据库

读写数据库的方式有多种,但不管是哪种方式读写数据库,都会包含一些相同的步骤,具体包括如下:

1 添加依赖:


	4.0.0
	com.gm
	springboot-test
	war
	0.0.1-SNAPSHOT
	springboot-test Maven Webapp
	http://maven.apache.org

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.0.RELEASE
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			mysql
			mysql-connector-java
		
		
			org.apache.poi
			poi
			3.11
		
	
	
		springboot-test
	


2 数据库配置:

在src/main/resources目录下简历文件application.properties文件(SpringBoot会使用一些默认的配置,当SpringBoot检测到application.properties文件时,会使用该文件中的参数来进行相关信息的配置),文件内容如下:

spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver

特别注意:每一行的后面不能有空格。


读取数据库的方法:

方法一:继承CrudRepository,用CrudRepository中提供的方法来读写数据库,举例如下:

1 编写数据库操作的实体类User:

package com.gm.springboot_test.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	@Column(name = "name", nullable = true, length = 255)
	private String name;

	@Column(name = "age", nullable = true, length = 11)
	private int age;

	@Column(name = "sex", nullable = true, length = 2)
	private String sex;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
	public String toString() {
		return "{name = " + name + 
				", age = " + age +
				", sex = " + sex + "}";
	}

}

2 编写dao层的数据库操作类UserDao

package com.gm.springboot_test.dao;

import javax.transaction.Transactional;

import org.springframework.data.repository.CrudRepository;

import com.gm.springboot_test.entity.User;

@Transactional
public interface UserDao extends CrudRepository {

}

注意:CrudRepository需要传两个类,其中的第一个类User表示数据库操作实体类,第二类Integer表示实体User中id对应的类型。


3 编写控制类UserController

package com.gm.springboot_test.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gm.springboot_test.dao.UserDao;
import com.gm.springboot_test.entity.User;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserDao userDao;
	
	@RequestMapping("/getAll")
	@ResponseBody
	public String getAll() {
		List userList = (List) userDao.findAll();
		
		String result = "";
		
		for (User user : userList) {
			result += user.toString() + "
"; } return result; } }
4 主程序入口类AppMain

package com.gm.springboot_test;

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

@SpringBootApplication
public class AppMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(AppMain.class, args);
	}

}



你可能感兴趣的:(SpringBoot,SpringBoot,数据库)