Hello你們好,本章咱們搭建項目基礎框架結構和整合mybatis;項目所需環境jdk1.8,maven,mysql數據庫;開發工具IDEA。有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝
DemoApplicttion.java爲項目的啓動類
java
application.properties爲項目配置文件mysql
pom.xml主要描述了項目的maven座標,依賴關係,開發者須要遵循的規則,缺陷管理系統,組織和licenses,以及其餘全部的項目相關因素,是項目級別的配置文件(說人話:項目所需jar包的管理)git
在application.properties文件中添加以下數據庫配置github
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=數據庫用戶名
spring.datasource.password=數據庫密碼
spring.datasource.driverClassName=com.mysql.jdbc.Driver複製代碼
CREATE TABLE `user_info` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 複製代碼
Model
web
package com.example.demo.model;
import javax.persistence.Column;
import javax.persistence.Id;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 11:55
*/
public class UserInfo {
/**
* 主鍵
*/
private String id;
/**
* 用戶名
*/
private String userName;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
複製代碼
Mapperspring
<?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.example.demo.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.example.demo.model.UserInfo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
</resultMap>
<sql id="Base_Column_List">
id,user_name
</sql>
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_info
where id = #{id,jdbcType=VARCHAR}
</select>
</mapper>
複製代碼
Daosql
package com.example.demo.dao;
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Param;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 11:54
*/
public interface UserInfoMapper {
UserInfo selectById(@Param("id") Integer id);
}複製代碼
Service數據庫
package com.example.demo.service;
import com.example.demo.model.UserInfo;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 11:56
*/
public interface UserInfoService {
UserInfo selectById(Integer id);
}複製代碼
ServiceImplapache
package com.example.demo.service.impl;
import com.example.demo.dao.UserInfoMapper;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 11:56
*/
@Service
public class UserInfoServiceImpl implements UserInfoService{
@Resource
private UserInfoMapper userInfoMapper;
public UserInfo selectById(Integer id){
return userInfoMapper.selectById(id);
}
}複製代碼
Controller瀏覽器
package com.example.demo.controller;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 11:39
*/
@RestController
@RequestMapping("userInfo")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@PostMapping("/hello")
public String hello(){
return "hello SpringBoot";
}
@PostMapping("/selectById")
public UserInfo selectById(Integer id){
return userInfoService.selectById(id);
}
}複製代碼
Controller中@RestController註解的做用:
@RestController是由@Controller和@ResponseBody組成,表示該類是controller和返回的結果爲JSON數據,不是頁面路徑。
重點看一下configurer中的MyBatisConfigurer.java
package com.example.demo.core.configurer;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
/**
* @ClassName: MybatisConfigurer
* @Description: Mybatis配置
* @author 張瑤
* @date 2018年1月20日 下午4:03:46
*
*/
@Configuration
public class MybatisConfigurer {
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage("com.example.demo.model");
// 添加XML目錄
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return factory.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.example.demo.dao");
return mapperScannerConfigurer;
}
}
複製代碼
@Configuration表示該文件是一個配置文件
@Bean表示該方法是一個傳統xml配置文件中的<Bean id=""></Bean>
其中factory.setTypeAliasesPackage("com.example.demo.model")表示項目中model的存儲路徑;
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));表示mapper.xml存儲路徑;
mapperScannerConfigurer.setBasePackage("com.example.demo.dao");表示dao層的存儲路徑
找到DemoApplication,右鍵,選擇run DemoApplication
控制檯打印以下信息即爲啓動成功
打開postman(Google瀏覽器插件,能夠去應用商店下載),輸入
注意修改本身的ip;出現如下結構數據即訪問成功
{
"id": 1,
"userName": "1"
}複製代碼
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
框架搭建,整合mybatis已完成,後續功能接下來陸續更新,有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝你們。