1. 程式人生 > >idea+Spring Boot之springboot+mysql+mybatis搭建

idea+Spring Boot之springboot+mysql+mybatis搭建

springboot的優勢之一就是快速搭建專案,省去了自己匯入jar包和配置xml的時間,使用非常方便。

1、選擇file>new>project建立專案project,然後選擇Spring initializr,點選下一步 


2、按圖示進行勾選,點選下一步,給專案起個名字,點選確定。


3、選擇左邊的列web,右邊列勾選web框 


4、如果前面沒有修改工程名稱,可以在這一介面更改,然後點選finish。


5、專案開始生成,介面右下角回彈出一個小方框,點選Enable Autop-Import ,idea 會自動下載jar包,時間比較長 (5分鐘左右)


6、等待片刻後,專案格式如下圖。


7、構建pom 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</
groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- alibabadruid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

8、建立application.yml檔案

server:
  port: 8080
spring:
    datasource:
        name: test
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://192.168.3.164:3307/kuangke?useUnicode=true&characterEncoding=utf-8&useSSL=true
        username: root
        password: root
        # 使用druid資料來源
type: com.alibaba.druid.pool.DruidDataSource
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
mybatis:
  mapper-locations: classpath*:mybatis/mapper/*.xml

9、user實體類

public class User {

    private String id;
    private String name;
    private String sex;
    private String age ;
    private String memo;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }
}

10 、建立Usermapper 介面

//@Mapper         //宣告是一個Mapper,springbootApplication中的@MapperScan二選一寫上即可
@Repository
public interface UserMapper {

    List<User> getAllUser();
}

11、建立介面 UserService 

public interface UserService {

    List<User> getAllUser();

}

12 、建立UserServiceImpl

@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Resource
private UserMapper userMapper;


    @Override
public List<User> getAllUser() {
        return userMapper.getAllUser();
    }
}

13 、在resource目錄下建立mybatis檔案下、在該檔案下建立mapper資料夾、然後在該資料夾下建立UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yangkai.mapper.UserMapper">

    <select id="getAllUser" resultType="com.yangkai.entity.User">
        SELECT * FROM user
    </select>

</mapper>

14 、建立controller 

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
private UserService userService;


    @RequestMapping("/getAllUser")
    @ResponseBody
private List<User> getAllUser() {
        List<User> users =  userService.getAllUser();

        return users;

    }

}

15、最後附上啟動器的程式碼

@SpringBootApplication
@EnableTransactionManagement   //開啟事務管理
@ComponentScan("com.yangkai")
@MapperScan("com.yangkai.mapper")//dao層的@Mapper二選一寫上即可(主要作用是掃包)
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }


    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

}

16、專案結構圖


17、啟動專案在瀏覽器輸入:http://localhost:8080/user/getAllUser ,如下圖 


原始碼下載:https://download.csdn.net/download/qq_36245532/10515363