springboot+mybatis初试深浅

本想找一个springmvc+mybatis的纯注解配置模板,然而百度的竟不如人意,再三挣扎之下,投入了springboot这个微框架的怀抱;在这里详解一下第一次配置springboot遇到的一些问题。

参考了众多的博客解决问题,但大多是昨天写的,所有具体是那些博客我也记不清了。

1.创建的是maven的简单工程,追求最简单的配置,项目基本结构就是这样

2.在根目录新建pom.xml文件

导包的时候遇到一些坑,最好是把spring-boot-starter-parent包导入进来,不然会遇到启动了项目但并未发布到tomcat的问题,具体导入4个关键包就好,看到的一些配置范本没有导入parent包,自己试了反正是不想,以后熟悉了找到什么问题在回来改

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
3.springboot的注解方式和spring差不多,我就简单的写了3个分层的文件

1.)entity

package study.entity;

/**
 * Created by Administrator on 2017/8/17.
 */
public class Sheep {
    private int id;
    private String name;
    private int age;
    private String sex;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

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

package study.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import study.entity.Sheep;

import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */
    public interface SheepMapper {
    @Insert("insert into sheep(name,age,sex) values(#{name},#{age},#{sex}) ")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")   //id自增长
    public void save(Sheep sheep);

    @Delete("delete *from sheep where id =#{id}")
    public void delete(int id);

    @Select("select *from sheep")
    public List<Sheep> findAll();

    @Select("select *from sheep where age = #{age}")
    public List<Sheep> getByAge(int age);


}
3.)service

package study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.dao.SheepMapper;
import study.entity.Sheep;

import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */
@Service
public class SheepService {
    @Autowired
    private SheepMapper sheepMapper;

    public List<Sheep> getByAge(int age) {
        return sheepMapper.getByAge(age);
    }
}
4.)controller,@RestController注解是@RequestBody和@Controller的结合,这里用@Controller也可以

package study.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.entity.Sheep;
import study.service.SheepService;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */
@RestController
public class SheepController {

    @Resource
    private SheepService sheepService;

    @RequestMapping("/getByAge")
    public List<Sheep> getByAge(int age) {
        return sheepService.getByAge(age);
    }

    @RequestMapping("/")
    public String home(){
        return "fuck!";
    }
}


3.还有就是一个Application的项目启动类,在这个类上也碰到个有意思的问题,这个类放到三层目录下的任何一个目录项目都无法正确启动,关键在MapperScan这个扫描mapper的注解,找到的资料是说不能扫描当前目录的上级目录。最好是把application类放到mapper类的上一级目录,扫描的时候扫dao层或直接扫根目录。至于mvc的一些注解,估计springboot项目默认支持

package study;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Administrator on 2017/8/16.
 */
@SpringBootApplication
@MapperScan("study.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}


4.这样项目基本就搭建好了,最后用到的mysql还需要在配置文件里配置一些sql的相关属性,application.properties是springboot的默认配置文件,这样项目启动会扫描里面spring.datasource.*开头的数据库配置

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456


5.最后是数据库,简单定义了几个属性,如果数据库字段和dto不一致的话,需要在dao层做字段映射



6.项目到这里就可以启动了,是的,刚接触springboot的时候也不知道它自带tomcat,还导成war包发到外部tomcat里,实际上application里的配置启动main方法就自动帮我么完成了,还找了下自带的tomcat更改端口或者时候外部tomcat发war包的方法,这里就不写了,百度一下很容易找到的,最后在浏览器打开是这样的


是不是感觉和springmvc+mybatis的长篇大论配置起来简单多了,接触springboot到现在正好一天,有什么错误请广大道友发现指出,还不清楚这个微框架能干多大的事情,不过已经感觉很有意思了,接下来如果找到值得写的东西会继续更springboot

7.导包的一些坑也在这里给大家说一下,之前用idea自带的导出jar包方式,也在.MF文件里指明了main方法所在地,然而并没有什么卵用,照样给你找不到主清单属性,最后还是用maven导出的方式成功了,需要导入这两个文件

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.0.M2</version>
            <type>maven-plugin</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin </artifactId>
            </plugin>
        </plugins>
    </build>

然后在maven中导出jar包,直接java -jar 或者写个bat方便一些。











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值