码迷,mamicode.com
首页 > 编程语言 > 详细

创建springboot 标准工程(MyBatis)

时间:2018-11-12 20:57:06      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:XML   scan   list   oid   实现   component   app   tde   bubuko   

实现目标:

技术分享图片

技术分享图片

创建一个简单的数据库demo,表名mach,id为自增,mach_no为字符串

技术分享图片

 

 创建Maven的jar工程,pom.xml内容如下

<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.legion</groupId>
    <artifactId>standard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </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>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
    </dependencies>
</project>

创建常规的package

com.legion.controller

com.legion.dao

com.legion.pojo

com.legion.service

创建包com.legion,新建类MyBootDemo,其中有main函数

@SpringBootApplication
public class MyBootDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(MyBootDemo.class,args);
    }

}

注意:其他包必须和springboot入口的package同级,或者处于下一级。

看到了有的工程在此处使用了

@ComponentScan(basePackages="XX.YY")
@MapperScan(basePackages="XX.YY.ZZ")

 

在src/main/resources建立application.properties文件

#下面这一行指定Mapper xml文件位置,发现不配置也可以
mybatis.mapper-locations=classpath:com/legion/dao/*.xml spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456

 

下面的内容和标准的SSM区别不大,注意Mapper接口加上@Mapper注解。

技术分享图片

 

com.leigon.controller中UserController.java的内容

@RestController
public class UserController {
    @Autowired
    private MachineService ms;
    @RequestMapping("/all")
    public List<Machine> findAll(){
        return ms.findAll();
    }
    
    @RequestMapping("/hello")
    public String show() {
        return "hello,world";
    }
    
}

com.legion.dao

MachineMapper.java

@Mapper
public interface MachineMapper {
    public List<Machine> findAll();
}

MachineMapper.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.legion.dao.MachineMapper">
    <select id="findAll" resultType="com.legion.pojo.Machine">
        select * from mach
    </select>

</mapper>

com.legion.pojo中的Machine.java

public class Machine {
    private int id;
    private String mach_no;
//getter and setter
...
}

com.legion.service接口和实现

public interface MachineService {
    public List<Machine> findAll();
}


@Service
public class MachineServiceImpl implements MachineService {
    
    @Autowired
    private MachineMapper machine;
    @Override
    public List<Machine> findAll() {
        return machine.findAll();
    }

}

 

创建springboot 标准工程(MyBatis)

标签:XML   scan   list   oid   实现   component   app   tde   bubuko   

原文地址:https://www.cnblogs.com/legion/p/9948851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!