提交 dc313ecf 编写于 作者: W wenguang

整合Mybatis,引入Excel依赖包

上级 a35a8e4d
......@@ -33,5 +33,48 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Excel导入导出工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beat1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.we;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.we.mapper")
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
package com.we.controller;
import com.we.pojo.Student;
import com.we.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class MysqlTestController {
private String prefix = "/mysqltest";
@Autowired
private IStudentService studentService;
@GetMapping("/listtest")
public String selectAll(Model model) {
List<Student> stus = studentService.selectStuList();
//Map<String, Object> model= new HashMap<String, Object>();
//model.put("stus", stus);
model.addAttribute("stus", stus);
return prefix + "/listtest";
}
}
package com.we.mapper;
import com.we.pojo.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StudentMapper {
public List<Student> selectStuList();
}
package com.we.pojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Student {
private String stuid;
private String stuclass;
private String stuname;
private String stuage;
private String stusex;
private String stutel;
}
package com.we.service;
import com.we.pojo.Student;
import java.util.List;
public interface IStudentService {
public List<Student> selectStuList();
}
package com.we.service.impl;
import com.we.mapper.StudentMapper;
import com.we.pojo.Student;
import com.we.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> selectStuList() {
return studentMapper.selectStuList();
}
}
<?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.we.mapper.StudentMapper">
<resultMap type="com.we.pojo.Student" id="StudentResult">
<result property="stuid" column="stuid" />
<result property="stuclass" column="stuclass" />
<result property="stuname" column="stuname" />
<result property="stuage" column="stuage" />
<result property="stusex" column="stusex" />
<result property="stutel" column="stutel" />
</resultMap>
<sql id="selectStuVo">
select stuid, stuclass, stuname, stuage, stusex, stutel from student
</sql>
<select id="selectStuList" parameterType="com.we.pojo.Student" resultMap="StudentResult">
<include refid="selectStuVo"/>
<where>
<if test="stuid != null and stuid != '' "> and stuid = #{stuid}</if>
<if test="stuclass != null and stuclass != '' "> and stuclass = #{stuclass}</if>
<if test="stuname != null and stuname != '' "> and stuname = #{stuname}</if>
<if test="stuage != null and stuage != '' "> and stuage = #{stuage}</if>
<if test="stusex != null and stusex != '' "> and stusex = #{stusex}</if>
<if test="stutel != null and stutel != '' "> and stutel = #{stutel}</if>
</where>
</select>
</mapper>
\ No newline at end of file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>学生信息</title>
</head>
<body>
<table>
<tr th:each="stus : ${stus}">
<td th:text="${stus.stuid}"></td>
<td th:text="${stus.stuclass}"></td>
<td th:text="${stus.stuname}"></td>
<td th:text="${stus.stuage}"></td>
<td th:text="${stus.stusex}"></td>
<td th:text="${stus.stutel}"></td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册