作者:ddddd6192010_808 | 来源:互联网 | 2023-09-18 04:59
篇首语:本文由编程笔记#小编为大家整理,主要介绍了springboot 开发环境搭建相关的知识,希望对你有一定的参考价值。
软件151 张鑫栋
一、maven 包的导入
清单如下:
org.springframework.boot
spring-boot-starter-parent
1.3.1.BUILD-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
javax.persistence
persistence-api
1.0.2
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.7
spring-snapshots
http://repo.spring.io/snapshot
spring-milestones
http://repo.spring.io/milestone
spring-snapshots
http://repo.spring.io/snapshot
true
spring-milestones
http://repo.spring.io/milestone
以上为整个spring boot 构建简单web项目所需要的maven配置文件。
二、创建实体类
package com.whucker.platform.domain;
@Entity //实体
@Table(name="t_user") //指定表名
public class User {
@Id //定义主键id
@GeneratedValue(strategy=GenerationType.AUTO) //自增
private Long id;
private String name;
private int age;
private int gender;
//...此处省略get/set
}
Repository代码(此处使用的是spring data jpa,由于该文章主要讲解spring boot,所有不在此对jpa做解释)
package com.whucker.platform.repository;
public interface UserRepository extends CrudRepository{
}
接口定义
package com.whucker.platform.service;
public interface IUserService {
//添加
void save(User user);
//删除
void delete(Long id);
//修改
void update(User user);
//查询单个对象
User findOne(Long id);
//查询所有
List findAll();
}
接口实现
package com.whucker.platform.service.impl;
@Service(value="userService")
@Transactional
public class UserServiceImpl implements IUserService{
@Autowired
private UserRepository userRepository;
@Override
public void save(User user) {
this.userRepository.save(user);
}
}
控制层
package com.whucker.platform.controler;
@RestController
public class IndexManageAction {
@RequestMapping(value="/")
public String index(){
return "欢迎访问首页!";
}
}
三、Application类
ackage com.whucker.platform;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
配置文件
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-db
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update