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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36245532/article/details/80887498

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"?>
<!DOCTYPE mapper PUBLIC "-//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


展开阅读全文

没有更多推荐了,返回首页