文章清單
本系列将通過執行個體分别實作Springboot內建mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket。 具體文章系列如下: 一.SpringBoot內建執行個體系列-xml型單資料源mybatis 二.SpringBoot內建執行個體系列-xml型多資料源mybatis 三.SpringBoot內建執行個體系列-注解型單資料源mybatis 四.SpringBoot內建執行個體系列-注解型多資料源mybatis 五.SpringBoot內建執行個體系列-郵件email 六.SpringBoot內建執行個體系列-單資料源mongodb 七.SpringBoot內建執行個體系列-多資料源mongodb 八.SpringBoot內建執行個體系列-緩存redis 九.SpringBoot內建執行個體系列-資料庫cassandra 十.SpringBoot內建執行個體系列-定時任務scheduler 十一.SpringBoot內建執行個體系列-消息隊列kafka 十二.SpringBoot內建執行個體系列-消息推送websocket
上兩篇文章,小求分别內建mybatis-xml型單資料源和多資料源。本章将通過執行個體內建mybatis注解型單資料源。
1.執行個體需求
使用者資訊(使用者名、别名、密碼、性别、郵箱、建立時間),實作簡單增删改查
2.技術要點
2.1 mybatis的xml型和注解型異同
同:都是調用mapper接口來關聯資料庫; 異:xml型-mapper.xml檔案通過mapper的namespace屬性關聯mapper接口,相應的sql腳本直接在mapper.xml檔案實作。
注解型-通過mapper接口對應的方法通過mybatis注解來關聯和管理sql腳本。
更多資訊:http://www.yiibai.com/mybatis/interface_comment.html
2.2 springboot單資料源配置
spring.datasource.driverClassName:資料源驅動類 spring.datasource.url:資料源url spring.datasource.username:使用者名 spring.datasource.password:密碼
#mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
2.3 springboot與mybatis內建設定
@MapperScan("com.lm.mapper") 使用@MapperScan來掃描注冊mybatis資料庫接口類
3. 代碼實作
3.1 項目目錄結構
3.2 mapper接口
package com.lm.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.lm.entity.UserEntity;
import com.lm.enums.SexEnums;
/**
* mybatis中mapper接口
* @author liangming.deng
* @date 2017年6月21日
*
*/
public interface UserMapper {
@Select("select * from user")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="userName",column="userName"),
@Result(property="nickName",column="nickName"),
@Result(property="passWord",column="passWord"),
@Result(property="email",column="email"),
@Result(property="regTime",column="regTime"),
@Result(property="sexEnums",column="sex",javaType=SexEnums.class)})
List<UserEntity> getAll();
@Select("select * from user where id=#{id}")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="userName",column="userName"),
@Result(property="nickName",column="nickName"),
@Result(property="passWord",column="passWord"),
@Result(property="email",column="email"),
@Result(property="regTime",column="regTime"),
@Result(property="sexEnums",column="sex",javaType=SexEnums.class)})
UserEntity getUserById(Long id);
@Insert("INSERT INTO user(userName,nickName,passWord,email,regTime,sex) "
+ "VALUES(#{userName}, #{nickName}, #{passWord}, #{email}, #{regTime}, #{sex}) ")
void insert(UserEntity user);
@Update("UPDATE user SET userName=#{userName},nickName=#{nickName} where id=#{id}")
void update(UserEntity user);
@Delete("DELETE FROM user where id=#{id}")
void delete(Long id);
}
整個mybatis注解型的實作,主要工作任務是集中在mapper接口。
3.3 springboot啟動器
package com.lm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 應用啟動
*
* @author liangming.deng
* @date 2017年6月20日
*
*/
@SpringBootApplication
@MapperScan("com.lm.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.6 運作效果
4. 代碼位址
Github: https://github.com/a123demi/spring-boot-integration