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-webapplication.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;