整体的项目结构,遵循MVC架构。红框内的是此次需要用到的文件。
src/main/java/com/zhu/DemoApplication.java
package com.zhu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
从后往前 Mapper层
src/main/java/com/zhu/mapper/UserMapper.java
package com.zhu.mapper;
import java.util.Collection;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.zhu.entity.User;
@Mapper
public interface UserMapper {
@Select("select * from car")
Collection<User> getAll();
Collection<User> selectOne(@Param("username") String username);
}
src/main/java/com/zhu/mapper/UserMapper.xml
<?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.zhu.mapper.UserMapper">
<select id="selectOne" resultType="com.zhu.entity.User">
select * from car
<where>
<if test="username !=null">
username = #{username}
</if>
</where>
</select>
</mapper>
Service层
src/main/java/com/zhu/service/UserService.java
package com.zhu.service;
import java.util.Collection;
import com.zhu.entity.User;
public interface UserService {
Collection<User> getAll();
Collection<User> selectOne(String username);
}
src/main/java/com/zhu/service/impl/UserServiceImpl.java
package com.zhu.service.impl;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhu.entity.User;
import com.zhu.mapper.UserMapper;
import com.zhu.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public Collection<User> getAll() {
return userMapper.getAll();
}
@Override
public Collection<User> selectOne(String username) {
return userMapper.selectOne(username);
}
}
Controller层
src/main/java/com/zhu/controller/UserController.java
package com.zhu.controller;
import java.util.Collection;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.zhu.entity.User;
import com.zhu.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/select")
public String selectAll(@RequestParam(name = "name", required = false, defaultValue = "zhangsan") String name,Model model) {
Collection<User> users = userService.getAll();
model.addAttribute("users", users);
return "user";
}
@RequestMapping("/selectone")
public String selectOne(@RequestParam(name="name" ,required = false ,defaultValue = "lisi") String name,Model model){
Collection<User> users = userService.selectOne("zhangsan");
model.addAttribute("users", users);
return "user";
}
}
页面代码
src/main/resources/templates/user.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet" />
</head>
<body>
<div class="container">
<table class="table table-bordered table-condensed table-hover">
<tr>
<th>ID</th>
<th>UserName</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
</tr>
</table>
</div>
</body>
</html>
点击运行