spring boot学习笔记之与MyBatis集成——基于注解

一、创建项目,选择相关pom starter

二、application-dev.properties配置:

server.port=8888
#logging.file=G://mylog//log_1.log
#slogging.level.org.springframework.web=DEBUG

#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=sx96411
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

三、JavaBean:

package com.wisely.stuMybatis.bean;


public class UserInfo {
	
	private Integer id;
	private String name;
	private String gender;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "UserInfo [id=" + id + ", name=" + name + ", gender=" + gender + "]";
	}
	
}

四、控制器:

package com.wisely.stuMybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisely.stuMybatis.bean.UserInfo;
import com.wisely.stuMybatis.service.UserInfoService;

@Controller
@RequestMapping("/userinfo/")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	
	@RequestMapping("index")
	public String index() {
		return "index";
	}
	
	@ResponseBody
	@RequestMapping("show/{id}")
	public UserInfo show(@PathVariable Integer id) {
		UserInfo userInfo = userInfoService.findById(id);
		//model.addAttribute("user",userInfo);
		return userInfo;
	}
}

五、Dao层:

package com.wisely.stuMybatis.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.wisely.stuMybatis.bean.UserInfo;

@Mapper
public interface UserInfoDao {
	@Select("select * from user_info where id = #{id}")
	@Results(value= {
			@Result(column="id",property="id"),
			@Result(column="name",property="name"),
			@Result(column="gender",property="gender")
	})
	public UserInfo findById(Integer id);

}

六、service层:

package com.wisely.stuMybatis.service;

import com.wisely.stuMybatis.bean.UserInfo;

public interface UserInfoService {
	public UserInfo findById(Integer id);

}

七、service实现层:

package com.wisely.stuMybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wisely.stuMybatis.bean.UserInfo;
import com.wisely.stuMybatis.dao.UserInfoDao;
import com.wisely.stuMybatis.service.UserInfoService;

@Service
public class UserInfoServiceImpl implements UserInfoService{

	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public UserInfo findById(Integer id) {
		System.out.println("查找id = "+ id +"的数据");
		UserInfo userInfo = userInfoDao.findById(id);
		System.out.println(userInfo);
		return userInfo;
	}
	

}

八、前端页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>index.html</h1>
<a href="/userinfo/show/3">查看用户信息</a>
 </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值