bootspring插⼊⼀条数据_SpringBoot-Insert
SpringBoot-Insert
SpringBoot连接数据库并插⼊数据
创建数据库表
在test数据库下新建test表如下,有id和name两个属性,其中id设为⾃增:
新建项⽬
Eclipse中创建Maven Project,输⼊相应的包名及项⽬名,这⾥的项⽬名是Insert,包名为com,接着使⽤如下代码替换l⾥⾯的内容:
xmlns:xsi="/2001/XMLSchema-instance"
4.0.0
1.0.0
dms-get-ecall
0.0.1-SNAPSHOT
jar
springboot-restful
UTF-8
1.7
1.2.0
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-data-jpa
mybatis-spring-boot-starter
${mybatis-spring-boot}
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
true
在src/main下新建resources⽂件夹,新建application.properties⽂件,将如下内容复制并修改相关信息,主要内容是数据库的连接配置,包括url、数据库⽤户名、密码和之前创建的数据库表名:
banner.charset=UTF-8
ding.charset=UTF-8
abled=true
ding.force=true
server.port=8083
spring.datasource.url=jdbc:mysql://数据库地址:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=数据库⽤户名
spring.datasource.password=数据库密码
spring.datasource.sql.jdbc.Driver
将com.Insert下的App.java代码更新如下:
package com.Select;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "Hello World!" );
}
}
创建后的项⽬结构如下:
完成之后右键项⽬,选择Maven/Update Projects更新项⽬。
创建项⽬
entity层
在src/main/java下新建entity包,创建User实体类,该实体类封装的变量对应test数据库表中的属性,代码如下: package ity;
//创建test表对应的实体类
public class User {
private int id;
private String name;
public User(String name2) {
// TODO Auto-generated constructor stub
this.name = name2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
dao层
jdbctemplate insert
在src/main/java下新建dao包,新建UserDao接⼝,实现对数据库的插⼊操作:
package com.Insert.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.JdbcTemplate;
import org.springframework.stereotype.Repository;
import ity.User;
//交给springboot管理的注解
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void Insert(User user) {
// TODO Auto-generated method stub
jdbcTemplate.update("insert into test(name) values(?)", Name());
}
}
service层
在src/main/java下新建service包,新建UserService接⼝,添加要⽤到的⽅法:
package com.Insert.service;
import ity.User;
public interface UserService {
void Insert(User user);
}
在src/main/java下新建serviceimpl包,新建UserServiceImpl类来实现接⼝,重写UserService中声明的⽅法:package com.Insert.serviceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Insert.dao.UserDao;
import ity.User;
import com.Insert.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public void Insert(User user) {
// TODO ⾃动⽣成的⽅法存根
userDao.Insert(user);
}
}
controller层
在src/main/java下新建controller包,新建UserController类来实现调⽤:
package ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ity.User;
import com.Insert.serviceimpl.UserServiceImpl;
@RestController
public class UserController {
@Autowired
private UserServiceImpl userservice;
@RequestMapping("/insert")
public String Insert(String name) {
User user = new User(name);
userservice.Insert(user);
return "insert successfully!";
}
}
⾄此,整个项⽬已搭建完毕,整体的框架如下:
测试运⾏
右键App.java,选择运⾏⽅式-Java应⽤程序,如果控制台会出现Hello World!则表明映射成功,并且能正常运⾏。然后打开postman设置相关信息如下,点击send之后会成功显⽰insert successfully!:
接着去数据库查看数据是否成功插⼊(运⾏了好多次):
OVER!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。