SpringBoot+Mybatis+Thymeleaf的简单示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42969074/article/details/85202004

花了点时间做了SpringBoot+Mybatis+Thymeleaf的简单示例,同样是在SpringBoot学习记录(三)——集成Mybatis的基础上改的。只有用过之后,印象才能更加深刻,记录一下学习的过程,以备后续使用。

项目整体目录:刚刚学习SpringBoot没多久,所以用properties比较习惯,这里就没用yml了。

1、依赖引入

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

一般在创建的时候就选择好了。

2、配置文件

server.port=8080

#Thymeleaf配置
#禁用Thymeleaf的缓存
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html


#mapper文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#对应实体类的位置
type-aliases-package=com.thr.entity 

#配置数据源
spring.datasource.username=root
spring.datasource.password=root
#使用druid数据源
#type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8

使用thymeleaf的时候一般会禁用它的缓存,不然更新数据的时候页面还是那个数据。 

3、类

Controller类:

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //查所有
    @RequestMapping(value = "/list")
    public String list(Model model){
        List<User> userList=userService.list();
        model.addAttribute("userList",userList);
        return "list";
    }

    //添加的跳转
    @RequestMapping(value = "/addIndex")
    public String addIndex(){
        return "add";
    }

    //添加
    @RequestMapping(value = "/add")
    public String add(User user){
            userService.add(user);
            return "redirect:/list";
    }

    //跳到修改页面
    @RequestMapping(value = "/toUpdate")
    public String toUpdate(Integer id, Model model){
        User user=userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }

    //修改
    @RequestMapping(value = "/udpate")
    public String update(User user){
        userService.update(user);
        return "redirect:/list";
    }

    //删除
    @RequestMapping(value = "/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/list";
    }
}

controller层使用普通的注解,没有使用@RestController。因为需要跳转页面,如果使用@RestController无法跳转页面。对外提供接口话,可以使用@RestController注解。

thymeleaf页面参数的传递可以使用这几种方式:request、ModelMap、Model

UserDao和UserService是一样的:

public interface UserDao {

    List<User> list();

    int add(User user);

    int update(User user);

    int delete(Integer id);

    User findById(Integer id);
}

UserMapper.xml配置文件:因为我本人不是很喜欢使用Mybatis的注解,所以这里使用了xml。

<mapper namespace="com.thr.dao.UserDao" >

  <resultMap id="BaseResultMap" type="com.thr.entity.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>

    <select id="list" parameterType="com.thr.entity.User" resultMap="BaseResultMap">
        select * from t_user
    </select>

    <insert id="add" parameterType="com.thr.entity.User">
        insert into t_user values (null,#{name},#{password})
    </insert>

    <update id="update" parameterType="com.thr.entity.User">
        update t_user set name=#{name},password=#{password} where id=#{id}
    </update>

    <delete id="delete" parameterType="Integer" >
        delete from t_user where id=#{id}
    </delete>

    <select id="findById" parameterType="Integer" resultMap="BaseResultMap">
        select * from t_user where id=#{id}
    </select>
</mapper>

实体:

public class User {
    private Integer id;
    private String name;
    private String password;

Service的实现:

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> list() {
        return userDao.list();
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

4、页面

list.html:

<body>

    <a th:href="@{/addIndex}">add</a>

    <table border="1" cellspacing="0">
        <tr>
            <td>id</td>
            <td>name</td>
            <td>password</td>
            <td>operation</td>
        </tr>
        <tr th:each="user:${userList}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}">name</td>
            <td th:text="${user.password}">password</td>
            <td>
                <a th:href="@{/toUpdate(id=${user.id})}">edit</a>
                <a th:href="@{/delete(id=${user.id})}">delete</a>

            </td>
        </tr>
    </table>
</body>

虽然thymeleaf用的是HTML页面,但是却拥有Jsp更加强大的功能。

add.html:

<body>

<form th:action="@{/add}" method="post">
    <table>
        <tr>
            <td>name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>password</td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td><input type="submit" value="add"></td>
        </tr>
    </table>
</form>

</body>

update.html:

<form th:action="@{/udpate}" method="post">
    <table>
        <tr>
            <td><input type="hidden" name="id" th:value="${user.id}"></td>
        </tr>
        <tr>
            <td>name</td>
            <td><input type="text" name="name" th:value="${user.name}"></td>
        </tr>
        <tr>
            <td>password</td>
            <td><input type="text" name="password" th:value="${user.password}"></td>
        </tr>
        <tr>
            <td><input type="submit" value="udpate"></td>
        </tr>
    </table>
</form>

5、运行

然后运行启动类访问http://localhost:8080/list,还有千万要记得在SpringBoot的启动类上面加一个@MapperScan注解,这样SpringBoot才能找到mapper(dao)接口,千万记住!!!!!

运行的效果如下,由于没有加CSS样式,可能看上去有点丑,但是里面的增删改查功能都是完整的。

猜你喜欢

转载自blog.csdn.net/qq_42969074/article/details/85202004