IntelliJ IDEA+SpringBoot创建模拟接口小程序
来源:互联网 发布:淘宝删除宝贝怎么恢复 编辑:程序博客网 时间:2024/06/06 01:55
一、首先,设计好数据库表,并且填上测试数据
二、新建一个Idea程序,使用spring Initialzrxiangdao向导,选择web->web,SQL->jdbc,SQL->MySQL,并且添加gson依赖库
五、建立bean文件夹,建立User.java实体类
二、新建一个Idea程序,使用spring Initialzrxiangdao向导,选择web->web,SQL->jdbc,SQL->MySQL,并且添加gson依赖库
三、配置启动文件Application.Java
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(Application.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(Application.class);}}四、在application.properties文件中配置数据连接信息
#DB Configuration:spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/testspring.datasource.username = rootspring.datasource.password = root#JPA Configuration:spring.jpa.database=MySQLspring.jpa.show-sql=truespring.jpa.generate-ddl=truespring.jpa.hibernate.ddl-auto=update#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialectspring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MYSQL5Dialect
当然数据连接的配置也可以使用一个经过@Configration注解修饰过的配置文件,二者二选一。
@Configurationpublic class DataBaseConfig { @Bean public DriverManagerDataSource dataSource(){ DriverManagerDataSource driverManagerDataSource=new DriverManagerDataSource(); driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); driverManagerDataSource.setUrl("jdbc:mysql://192.168.1.101:3306/test"); driverManagerDataSource.setUsername("root"); driverManagerDataSource.setPassword("root"); return driverManagerDataSource; }}
五、建立bean文件夹,建立User.java实体类
@Entity@Table(name = "user")public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) public int id; public String name; @Column(name = "password") public String passWord;}六、创建UserSqls.java接口文件,几种放置SQL查询语句
public interface UserSqls { String SQL_GET_USER_LIST_BY_NAME="SELECT * FROM user WHERE name=?";//获取用户列表 String SQL_GET_USER_LIST="SELECT * FROM user";//获取所有用户列表}七、创建UserRepository.java接口文件
public interface UserRepository extends CrudRepository<User, Integer> { User findUserById(int id); @Query(value = UserSqls.SQL_GET_USER_LIST_BY_NAME, nativeQuery = true) List<User> getUserListByName(String name); @Query(value = UserSqls.SQL_GET_USER_LIST, nativeQuery = true) List<User> getUserList();}八、创建UserController.java文件,写几个接口方法
@RestControllerpublic class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/getUserInfo") public User getUserInfo(int id) { User user = userRepository.findUserById(id); return user; } @RequestMapping("/getUserListByName") public List<User> getUserListByName(String name) { List<User> userList = userRepository.getUserListByName(name); return userList; } @RequestMapping("/getUserList") public List<User> getUserList() { List<User> userList = userRepository.getUserList(); return userList; } @RequestMapping("/getUserListJson") public String getUserListJson() { List<User> userList = userRepository.getUserList(); return new Gson().toJson(userList); }}
九、配置Artifacs部署到本地Tomcat服务器(尝试部署到软件内部的Tomcat出错没解决)
十、调用
http://192.168.1.101:8080/myall/getUserList
返回
[{"id":1,"name":"kalychen","passWord":"123456"},{"id":2,"name":"will","passWord":"123456"}]
为APP写模拟几口程序研究,比较肤浅,只图够用。
阅读全文
0 0
- IntelliJ IDEA+SpringBoot创建模拟接口小程序
- IntelliJ IDEA搭建SpringBoot的小Demo
- IntelliJ IDEA搭建SpringBoot的小Demo
- IntelliJ IDEA搭建SpringBoot的小Demo
- IntelliJ IDEA搭建SpringBoot的小Demo
- Intellij IDEA创建 java 后端 SpringBoot项目
- 使用Intellij IDEA创建SpringBoot项目
- 使用Intellij创建第一个Springboot程序
- 使用Intellij创建第一个Springboot程序
- 使用Intellij创建第一个Springboot程序
- 使用Intellij创建第一个Springboot程序
- 使用Intellij创建第一个Springboot程序
- 使用Intellij创建第一个Springboot程序
- IntelliJ IDEA搭建SpringBoot
- IntelliJ IDEA搭建SpringBoot应用完成的一个小Demo
- 怎么用IntelliJ IDEA创建java程序
- IntelliJ IDEA搭建SpringBoot应用
- Intellij idea Springboot 热加载
- js中的匿名函数
- java语言实现:利用递归方法求n!
- http://blog.csdn.net/tonyshengtan/article/details/42150125
- Nginx Rewrite 规则相关指令
- Linux软链接与硬链接
- IntelliJ IDEA+SpringBoot创建模拟接口小程序
- 高精度--大数乘法 luogu
- HTML
- Angular基础(四) 创建Angular应用
- js
- httpClient4.5.3 代理设置账号
- Iterator迭代器
- hiho 最短的 Nore0061(思维)
- 原生JavaScript常用的DOM操作