SpringBoot之Mybatis篇(注解&配置文件)(MVC版)

整体的项目结构,遵循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>

点击运行

http://localhost:8080/select

这里写图片描述

http://localhost:8080/selectone

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值