添加pom支持
<!--Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
定义SysUserMapper.xml,默认地址 src/main/resources
<?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.duck.dao.SysUserMapper" >
<resultMap id="SysUserMap" type="com.duck.bean.SysUser" >
<id column="id" property="id"/>
<result column="real_name" property="realName"/>
<result column="login_name" property="loginName"/>
<result column="password" property="password"/>
</resultMap>
<select id="querySysUser" parameterType="java.util.Map" resultMap="SysUserMap">
select * from sys_user
</select>
</mapper>
定义dao,SysUserMapper
package com.duck.dao;
import com.duck.bean.SysUser;
import java.util.List;
import java.util.Map;
public interface SysUserMapper {
List<SysUser> querySysUser(Map param);
}
SysUserServiceImpl注入添加方法
package com.duck.service;
import com.duck.bean.SysUser;
import com.duck.dao.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@Service
public class SysUserServiceImpl implements SysUserService{
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
SysUserMapper sysUserDao;
//使用JdbcTemplate访问
public List<SysUser> querySysUser(Map map){
String sql = "select * from sys_user ";
return jdbcTemplate.query(sql, new RowMapper<SysUser>() {
@Override
public SysUser mapRow(ResultSet resultSet, int i) throws SQLException {
SysUser sysUser = new SysUser();
sysUser.setId(resultSet.getInt("id"));
sysUser.setLoginName(resultSet.getString("login_name"));
sysUser.setRealName(resultSet.getString("real_name"));
sysUser.setPassword(resultSet.getString("password"));
return sysUser;
}
});
}
//使用mybatis访问
public List<SysUser> qrySysUser(Map map){
List<SysUser> sysUserList = sysUserDao.querySysUser(map);
return sysUserList;
}
}
SysUserService接口添加方法
package com.duck.service;
import com.duck.bean.SysUser;
import java.util.List;
import java.util.Map;
public interface SysUserService {
List<SysUser> querySysUser(Map map);
List<SysUser> qrySysUser(Map map);
}
DuckController添加访问方法
package com.duck.controller;
import com.duck.bean.SysUser;
import com.duck.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//会返回jsp界面
@Controller
//RestController是Controller 与 ResponseBody的合体,返回的是json字符串
//@RestController
public class DuckController {
private static final Logger logger = LoggerFactory.getLogger(DuckController.class);
//取配置文件配置的参数(如果配置文件没有配置appplication.hello 那就取冒号后的值)
@Value("${application.hello:配置是空取我}")
private String hello = "";
@Autowired
SysUserService sysUserService;
@RequestMapping("/index")
public String index(Map<String, Object> model){
logger.info("index方法");
model.put("hello",hello);
model.put("message","SpringBoot项目的首页");
//返回字符串,框架会默认去目录spring.mvc.view.prefix定义的文件夹下找(字符串+spring,mvc.view.suffix)的jsp文件
return "index";
}
@RequestMapping("/freemarker")
public String freemarker(Map<String,Object> model){
logger.info("freemarker方法");
model.put("message","freemarker首页");
return "freemarker";
}
@RequestMapping("querySysUser")
public @ResponseBody List<SysUser> querySysUser(){
logger.info("JdbcTemplate 查询用户");
List<SysUser> sysUserList = sysUserService.querySysUser(new HashMap());
return sysUserList;
}
@RequestMapping("qrySysUser")
public @ResponseBody List<SysUser> qrySysUser(){
logger.info("Mybatis 查询用户");
List<SysUser> sysUserList = sysUserService.qrySysUser(new HashMap());
return sysUserList;
}
}
配置文件添加Mybatis实体类与xml文件路径
#Mybatis的实体与配置文件位置 mapperLocations的默认位置是src/main/resources
mybatis.typeAliasesPackage=com.duck.bean
mybatis.mapperLocations=classpath:xml/*Mapper.xml
启动类添加MapperScan,扫描dao
package com.duck;
import com.duck.servlet.DuckTestServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(scanBasePackages = {"com.duck"},exclude={})
//扫描工程中的Servlet Listener Filter
@ServletComponentScan(basePackages = {"com.duck"})
//mybatis框架中的dao扫描
@MapperScan("com.duck.dao")
public class DuckAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DuckAdminApplication.class, args);
}
/**
* 通过代码的形式注册一个Servlet,这种形式不需要添加@ServletComponentScan注解来扫描
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new DuckTestServlet(), "/duckTest/*");
}
}
访问路径 可得查询结果,是通过mybatis查询获取