1、在上文提到的整合项目中,添加依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- sqlserver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<build>
<finalName>channel</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2、在application.yml中配置mybatis和数据源等信息
#公共配置与profiles选择无关
mybatis:
typeAliasesPackage: com.ljb.channel.entity #实体类所在包
mapperLocations: classpath:mapper/*.xml #配置文件所在包
#开发配置
spring:
datasource:
url: jdbc:sqlserver://localhost:1433;databaseName=数据库名称
username: 用户名
password: 密码
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
...
3、创建关键目录如下
4、在APP.java类中添加注解
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.ljb.channel.dao")
public class ChannelApp
{
public static void main( String[] args )
{
SpringApplication.run(ChannelApp.class, args);
}
}
5、至此配置文件,创建好数据库写好controller即可访问,下面是核心代码
<?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.ljb.channel.dao.CategoryMapper" >
<resultMap id="BaseResultMap" type="com.ljb.channel.entity.Category" >
<id column="Id" property="id" jdbcType="VARCHAR" />
<result column="CATEGORYNAME" property="categoryname" jdbcType="NVARCHAR" />
<result column="CATEGORYDESC" property="categorydesc" jdbcType="NVARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
Id, CATEGORYNAME, CATEGORYDESC
</sql>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from T_CATEGORY
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from T_CATEGORY
where Id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectCategoryList" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List" />
from T_CATEGORY
where 1=1
</select>
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from T_CATEGORY
where CATEGORYNAME = #{categoryname}
</select>
...
</mapper>
package com.ljb.channel.dao;
import java.util.List;
import java.util.Map;
import com.ljb.channel.entity.Category;
public interface CategoryMapper {
List<Category> selectAll();
Category selectByPrimaryKey(String id);
Category selectByName(String categoryname);
List<Category> selectCategoryList(Map<String, Object> map);
}
package com.ljb.channel.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ljb.channel.entity.Category;
import com.ljb.channel.service.CategoryService;
import com.ljb.channel.util.Result;
import com.ljb.channel.util.UUIDTool;
@Controller
@RequestMapping("/categoryController")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping(value = "/queryCategory", method = RequestMethod.POST)
@ResponseBody
public Result queryCategory() {
Result result = new Result();
String msg = "";
Map<String, Object> map = new HashMap<String, Object>();
try {
Map<String, Object> fileps = new HashMap<String, Object>();//用于查询列表数据
List<Category> list = categoryService.getCategoryList(fileps);
map.put("data", list);//数据集合
}catch(Exception e) {
e.printStackTrace();
msg = "数据获取失败!";
}
result.setExtend(map);
result.setSuccess(true);
result.setMsg(msg);
return result;
}
}
package com.ljb.channel.service.impl;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ljb.channel.dao.CategoryMapper;
import com.ljb.channel.entity.Category;
import com.ljb.channel.service.CategoryService;
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
@Resource
CategoryMapper categoryMapper;
@Override
public List<Category> getCategoryList(Map<String, Object> map) {
List<Category> list = categoryMapper.selectCategoryList(map);
return list;
}
}
SpringBoot系列文章请查看,更多内容正在更新中
(三)Maven+SpringBoot整合thymeleaf
(四)Maven+SpringBoot+thymeleaf 配置热部署devtools
(五)Maven+SpringBoot整合Bootstrap
(六)Maven+SpringBoot整合Mybatis+SQL Server
(七)SprintBoot项目打包成jar并做成后台运行的服务