0
点赞
收藏
分享

微信扫一扫

jdbc template

NicoalsNC 2022-01-13 阅读 30

pom

org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web

application.yml新增配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver

UserService类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Boolean inserUser(String name, Integer age) {
int update = jdbcTemplate.update(“insert into users values(null,?,?);”, name, age);
return update > 0 ? true : false;
}
}

App类
@ComponentScan(basePackages = “com.mayikt”)
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) NOT NULL COMMENT ‘用户名称’,
age int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

举报

相关推荐

0 条评论