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

#mybatis
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.wisely.stuMybatis.bean

三、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 + "]";
	}
	
}

四、Mapper映射文件

<?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">
<!-- namespace指用户自定义的命名空间。 -->
<mapper namespace="com.wisely.stuMybatis.dao.UserInfoDao">

	<resultMap type="UserInfo" id="usermap">
  		<id column="id" property="id"/>
  		<result column="name" property="name"/>
  		<result column="gender" property="gender"/>
	</resultMap>

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

五、Controller:

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 com.wisely.stuMybatis.bean.UserInfo;

@Mapper
public interface UserInfoDao {

	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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值