SpringBoot 项目配置和连接MySql 数据库
官方文档参考链接:https://spring.io/guides/gs/accessing-data-mysql/
官方文档的例子写的很详细,建议看文档,实在看不懂的,可以往下看
安装的jdk 版本为:1.8
安装的maven版本为:3.3.9
安装和配置mysql数据库,jdk,maven请自己解决,
liunx系统安装mysql参考链接:http://blog.csdn.net/phpfzh/article/details/72879354
windows 系统安装mysql网上例子很多,自己找找
本次项目目录图:
application.properties代码:
server.context-path=/ROOT
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=j3550563
注意:自己mysql的用户名和密码,数据库一定要新建好。
pom.xml 代码:
实体类User 代码:
package com.phpfzh.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserRepository 代码:
package com.phpfzh.dao;
import org.springframework.data.repository.CrudRepository;
import com.phpfzh.model.User;
public interface UserRepository extends CrudRepository
}
UserController 代码:
package com.phpfzh.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.phpfzh.dao.UserRepository;
import com.phpfzh.model.User;
@Controller
@RequestMapping(path="/user")
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping(path = "/add")
public @ResponseBody String addNewUser(@RequestParam String name,@RequestParam String email){
User user = new User();
user.setEmail(email);
user.setUsername(name);
User user2 = userRepository.save(user);
System.out.println(user2);
return "success";
}
@GetMapping(path = "/all")
public @ResponseBody Iterable
return userRepository.findAll();
}
}
ApplicationCore 代码:
package com.phpfzh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationCore {
public static void main(String[] args) {
SpringApplication.run(ApplicationCore.class, args);
}
}
SpringServletInitializer 代码:
package com.phpfzh;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* 修改启动类,继承 SpringBootStartApplication 并重写 configure 方法
*/
public class SpringServletInitializer extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ApplicationCore.class);
}
}
编译项目,启动测试:
右击项目 点击 run As ==> Maven clear ====> maven install
右击项目 点击 run As ===> java application ===
浏览器输入:
1:http://localhost:8080/ROOT/user/add?name=张三&email=dfdfdsdfdsfdsds
2:http://localhost:8080/ROOT/user/all