1. 程式人生 > >idea spring-boot gradle mybatis

idea spring-boot gradle mybatis

apply 表名 ngs exc back cte org pty ram

使用工具idea 2017.2開發,gradle構建項目,使用的技術有spring-boot、mybatis

1、新建項目

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

說明:1、src為源碼路徑,開發主要在src下

    2、src/main/java下放java文件

    3、src/main/resources下放配置文件

    4、src/test/java下放test測試案例

    5、build.gradle文件:gradle配置文件

2.配置build.gradle文件

buildscript { // 第三方插件
ext {
springBootVersion = ‘1.5.6.RELEASE‘
}
repositories { // maven倉庫地址
maven{url ‘http://xxxx/‘} // 私庫
mavenCentral()
}
dependencies { // 依賴項
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

buildscript中的聲明是gradle腳本自身需要使用的資源

apply plugin: ‘java‘ // java項目
apply plugin: ‘eclipse‘ // eclipse開發環境構建,生成所需要的.project,.classpath等文件
apply plugin: ‘org.springframework.boot‘
jar {
baseName = ‘shop-supplier‘
version = ‘1.0.0-SNAPSHOT‘
}
version = ‘1.0.0-SNAPSHOT‘
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories { // maven倉庫地址
maven{url ‘http://maven.aliyun.com/nexus/content/groups/public/‘}
mavenCentral()
}

dependencies { // 依賴項
// web thymeleaf
compile(‘org.springframework.boot:spring-boot-starter-web‘)
compile(‘org.springframework.boot:spring-boot-starter-thymeleaf‘)

// test
testCompile(‘org.springframework.boot:spring-boot-starter-test‘)

//添加 google二維碼
compile ‘com.google.zxing:core:3.2.0‘
}
3.配置mybatis

技術分享


mybatis-config.xml:mybatis配置文件
mybatis/mapper文件夾下時mybatis的mapper.xml文件


3.application.properties:項目配置文件,內容如下:
# 數據庫配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
spring.datasource.username=root spring.datasource.password=1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis配置
mybatis.config-location=classpath:mybatis-config.xml // 配置文件位置
mybatis.typeAliasesPackage=com.my.domain // 實體類包
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml // mapper文件位置
#log
logging.file=log.log
logging.level.com=info
logging.level.org=info
logging.level.com.my=debug
debug=true
logging.level.com.my.web = debug

4.mapper文件

<?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.my.dao.xxxDao">
  <!-- 通用結果集 -->
  <resultMap id="BaseResultMap" type="com.my.domain.xxx">
    <id column="id" property="id"/>
    <result column="user_name" property="userName"/>
    <result column="字段" property="屬性"/>
  </resultMap>
  
  <!-- 插入 -->
  <insert id="方法名" paramType="com.my.domain.實體類">
    INSERT INTO 表名 (id, user_name) VALUES (#{屬性id}, #{屬性userName})
  </insert>

  <!-- 刪除 -->
  <delete ... >
  
  <!-- 修改 -->
  <update ... >

  <!-- 查詢 -->
  <select ... >
</mapper>

5.mybatis-config.xml配置設置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="SLF4J" />
</settings>
</configuration>

6.main方法入口

@SpringBootApplication // 入口註解
@MapperScan("com.my.dao") // mybatis掃描路徑
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

註解:1.controller註解:

    @RestCotroller類前--返回json 等價於 @Controller類前 + @ResponseBody方法前

[email protected](value = "/order" method = RequestMethod.GET)類前或方法前

[email protected],參數後面跟BindingResult參數,接收驗證信息

[email protected] @NotEmpty @Length @NotBlank @Min @Size @JsonFormat入參對象屬性前

   2.service:

[email protected]

[email protected](rollbackFor = Exception.class)方法前

   3.dao:

[email protected]

[email protected]

  

定時器:
[email protected]

[email protected]

[email protected](cron = "秒 分 時 天 月 星期 年"

一個cron表達式有至少6個(也可能7個)有空格分隔的時間元素。

按順序依次為

秒(0~59)

分鐘(0~59)

小時(0~23)

天(月)(0~31,但是你需要考慮你月的天數)

月(0~11)

天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

7.年份(1970-2099)

其中每個元素可以是一個值(如6),一個連續區間(9-12),一個間隔時間(8-18/4)(/表示每隔4小時),一個列表(1,3,5),通配符

    

idea spring-boot gradle mybatis