用IntelliJ的IDEA来创建SpringBoot框架

时间:2022-01-24 01:56:31

要安装ULTIMATE版本,并导入key

http://idea.iteblog.com/key.php

安装完成后

1:首先打开New Project
2:选择Spring Initializr 这里直接按Next
3:配置Metadata
Group:com.example
Arifact:demo|
这里直接按Next
4:勾选Web、MyBatis、MySQL然后Next

编辑文件application.properties,添加服务端口、数据库配置

server.port=8081
server.address=0.0.0.0 spring.datasource.url=jdbc:mysql://mysql.feieryun.cn/firadio_yun?verifyServerCertificate=false&useSSL=false
spring.datasource.username=feieryun
spring.datasource.password=feieryun

在DemoApplication.java的同级文件夹
也就是在\demo\src\main\java\com\example\demo下面
新建文件夹controller,然后在里面新建IndexController.java

package com.feieryun.ssproxy.controller;

import com.feieryun.ssproxy.model.bootstrapt.BaseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
public class IndexController {
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public BaseResult index() {
return new BaseResult(false, "Hello World!", false);
}
}

这个是BaseResult

package com.feieryun.ssproxy.model.bootstrapt;

import java.io.Serializable;

/**
*/
public class BaseResult implements Serializable { //private static final long serialVersionUID = -5231857415211397573L;
private boolean success;
private String msg;
private Object data; public BaseResult() {
} public BaseResult(boolean success, String msg, Object data) {
this.success = success;
this.msg = msg;
this.data = data;
} public boolean isSuccess() {
return success;
} public void setSuccess(boolean success) {
this.success = success;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

这里是DatabaseConfig的内容

package com.example.demo;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer; import javax.sql.DataSource; @Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.example.demo.mapper")
public class DatabaseConfig implements TransactionManagementConfigurer { @Autowired
DataSource dataSource; @Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setFailFast(true);
return sqlSessionFactory.getObject();
} @Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
} /**
* @fun 配置事务管理器
*/
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
} }