springboot与mybatis入门

在这个项目使用许多非常重要的注解

注解作用
@Mapper在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
@MapperScan指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
@Transactional是声明式事务管理,在实现类上使用

1.导入相关包

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
	</parent>
	<groupId>cn.liuhao</groupId>
	<artifactId>springboot-mybatis-01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!-- mybatis启动器 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.3</version>
		</dependency>
		<!-- jdbc -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.1</version>
		</dependency>
	</dependencies>
</project>

2.在配置文件中添加mybatis配置

在这里插入图片描述

配置文件内容如下

#数据连接参数
spring.datasource.driveClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ spide01?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定mybatis要起别名实体类所在的包
mybatis.type-aliases-package=cn.liuhao.spide_haotanku.pojo
#指定mapper映射文件的文件夹路径
mybatis.mapperLocations=classpath:mapper/*.xml

3.简单项目实现

3.1添加数据库

-- 创建数据库
create database ssm;
-- 使用ssm
use ssm;
-- 创建表
create table users
(
	id int not null auto_increment,
	name varchar(50) default null,
	age int(11) default null,
	constraint pk_user primary key(id)
);

insert into users values (default,'张三',18)

3.2 编写实体类

User

package cn.liuhao.web.pojo;

public class User {

	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public User(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}

}

3.3实现mybatis接口绑定技术(编写mapper和映射的xml)

userMapper

package cn.liuhao.web.mapper;

import java.util.List;

import cn.liuhao.web.pojo.User;

public interface UserMapper {

	public int insert(User u);

	public List<User> selectAll();
}

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="cn.liuhao.web.mapper.UserMapper">
	
	<insert id="insert" parameterType="user">
		insert into users values (default,#{name},#{age})
	</insert>
	
	<select id="selectAll" resultType="user">
		select * from users
	</select>
</mapper>

3.4编写service业务层

interface

package cn.liuhao.web.service;

import java.util.List;

import cn.liuhao.web.pojo.User;

public interface UserService {

	public List<User> getUsers();

	public int addUser(User u);
}

implement

package cn.liuhao.web.service.impl;

import java.util.List;

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

import cn.liuhao.web.mapper.UserMapper;
import cn.liuhao.web.pojo.User;
import cn.liuhao.web.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	UserMapper userMapper;

	@Override
	public List<User> getUsers() {

		return userMapper.selectAll();
	}

	@Override
	public int addUser(User u) {

		return userMapper.insert(u);
	}

}

3.5 controller

package cn.liuhao.web.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 cn.liuhao.web.pojo.User;
import cn.liuhao.web.service.UserService;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping("{page}")
	public String page(@PathVariable("page") String page) {

		return page;
	}

	@RequestMapping("showUser")
	@ResponseBody
	public Object showUsers() {

		return userService.getUsers();
	}

	@RequestMapping("addUser")
	public String addUser(String uname, String uage) {
		userService.addUser(new User(uname, Integer.parseInt(uage)));
		return "ok";
	}
}

3.6 视图模板

register.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form th:action="@{/user/addUser}" method="post">
		<input type="text" name="uname"  />
		<input type="text" name="uage" />
		<input type="submit" value="添加" />
	</form>
</body>
</html>

ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>添加成功</h2>
</body>
</html>

3.7 启动类

package cn.liuhao;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 * mapperScan 用来指定扫描mybatis映射文件
 * 
 * @author admin
 *
 */
@SpringBootApplication
@MapperScan("cn.liuhao.web.mapper")
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值