在这个项目使用许多非常重要的注解
注解 作用 @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>
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 2.1.3</ version>
</ dependency>
< 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;
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;
@SpringBootApplication
@MapperScan ( "cn.liuhao.web.mapper" )
public class App {
public static void main ( String[ ] args) {
SpringApplication. run ( App. class , args) ;
}
}