该案例是使用IDEA创建的Maven的web项目
mybatis操作数据库有两种方式:一种是注解方式(dao层),一种是xml文件方式(记得在application.properties中读取)。
本案例是以注解方式读取
步骤一:在pom.xml中导入依赖
<!--声明springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- mvc,aop的依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 由于我们在上面指定了parent,这里就不需要指定版本号 -->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
步骤二:在resources下创建application.properties属性文件,配置数据库信息
#controller那return跳转页面是,页面默认前缀目录。/代表从webapp下找
spring.mvc.view.prefix=/
#controller那return跳转页面是,响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 连接mysql数据库
spring.datasource.url=jdbc:mysql://localhost:3306/day06
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
以上框架springboot+jsp页面+mybatis操作数据库框架就搭建完了下面是案例
步骤三:创建数据库表对应的实体类
本案例将该类命名为:Users
package com.qf.entity;
import java.util.Date;
public class Users {
private Integer userId;
private String username;
private Date birthday;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", username='" + username + '\'' +
", birthday=" + birthday +
", age=" + age +
'}';
}
}
步骤四:在dao层创建操作数据库的sql语句
该案例将该类命名为:UsersDao
package com.qf.dao;
import com.qf.entity.Users;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//mybatis框架操作数据库有两种方式,一种是配置文件方式,一种是注解方式
public interface UsersDao {
@Select("select userId,username,birthday from users")//mybatis的sql注解。因为userId,username,birthday与数据库字段名一致才这么写的。不一致时对应规则,参见mybatis框架。此案例用注解实现对数据库的操作.一旦加了注解,记得价格xml文件进行扫描
public List<Users> getall();//这里只有加了Users泛型他才知道自动对应这个方法。
}
步骤五:在service层创建接口和调用dao层方法的实现类
该案例将该接口命名为:UsersService
package com.qf.service;
import com.qf.entity.Users;
import java.util.List;
public interface UsersService {
public List<Users> getall();
}
该案例将该实现类命名为:UsersServiceImpl
package com.qf.service.impl;
import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService{
@Resource
private UsersDao usersDao;
@Override
public List<Users> getall() {
return usersDao.getall();//调用dao层的getall方法,或许sql执行结果
}
}
步骤六:在controller定义操作数据库数据和页面显示的类
该案例将该类命名为:TestController
package com.qf.controller;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class TestController {
@Resource
private UsersService usersService;
@RequestMapping("/test")//访问此请求的地址是localhost:8080/test
public String test(ModelMap map){
System.out.println("testjsp-----------");
List<Users> usersList = usersService.getall();
map.addAttribute("userList",usersList);
return "show";//此时实际跳去的页面时/show.jsp.
}
}
步骤七:定义启动类(一定要定义在在controller类的父包才行)
本案例将该类命名为:StartBegin
package com.qf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类一定要定义在controller类的父包中,也就是不在同一包中的上一级包
@SpringBootApplication//启动类的标志,必须在启动类上面加
@MapperScan("com.qf.dao")//@MapperScan()扫描我们在dao层定义的注解写的sql语句。参数时接口的包路径(不包含接口自己)
public class StartBegin {
public static void main(String[] args) {
SpringApplication.run(StartBegin.class,args);//StartBegin代表本类对象
}
}
步骤八:创建controller用到的jsp页面
本案例将该页面命名为:show.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; charset=utf-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>TestController的test请求跳转成功</h1>
<h1></h1>
<c:forEach items="${userList}" var="Users">
${Users.userId}---${Users.username}---${Users.birthday}
</c:forEach>
</body>
</html>
本案例结构图:
重点代码:一、二步中的代码以及以下截图: