1、创建数据库及数据表
CREATE
DATABASE /*!
32312
IF
NOT
EXISTS*/
`springbootdb` /*!
40100
DEFAULT
CHARACTER
SET utf8 */;
USE `springbootdb`;
/*Table structure for table `user` */
DROP
TABLE
IF
EXISTS
`user`;
CREATE
TABLE
`user` (
`id`
int(
11)
NOT
NULL,
`username`
varchar(
16)
DEFAULT
NULL,
PRIMARY
KEY (
`id`)) ENGINE=InnoDB
DEFAULT CHARSET=utf8;
我的项目结构图
2,创建User.java
package com.spb.SpringBootDemo.vo;
public
class
User {
private
String id;
private
String username;
public
String
getId
() {
return
id; }
public
void
setId
(String id) {
this
.id = id; }
public
String
getUsername
() {
return
username; }
public
void
setUsername
(String username) {
this
.username = username; }}
3、在pom.xml中加入以下依赖
<properties
>
<mybatis-spring-boot
>
1.2.0
mybatis-spring-boot
>
<mysql-connector
>
5.1.39
mysql-connector
>
properties
>
<
dependency
>
<
groupId
>
org.springframework.boot
groupId
>
<
artifactId
>
spring-boot-starter-web
artifactId
>
dependency
>
<
dependency
>
<
groupId
>
org.springframework.boot
groupId
>
<
artifactId
>
spring-boot-starter-test
artifactId
>
<
scope
>
test
scope
>
dependency
>
<
dependency
>
<
groupId
>
org.mybatis.spring.boot
groupId
>
<
artifactId
>
mybatis-spring-boot-starter
artifactId
>
<
version
>
${mybatis-spring-boot}
version
>
dependency
>
<
dependency
>
<
groupId
>
mysql
groupId
>
<
artifactId
>
mysql-connector-java
artifactId
>
<
version
>
${mysql-connector}
version
>
dependency
>
4, 在resource下打开application.properties文件,mysql和mybatis可以根据自己的地址配置。
spring.datasource.url
=
jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username
=
root
spring.datasource.password
=
leishao
spring.datasource.driver-class-name
=
com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package
=
com.spd.springbootdemo.vo
以上为mysql数据源相关配置,最后两行为mybatis需要扫描的model文件路径和mapper.xml文件路径
5.打开启动类加入相应mapper(dao)扫描注解
6、
新增UserDao,并在resources下面创建mapper目录,在下面创建UserMapper.xml
import
com.spd.springbootdemo.vo.User;
import
org.apache.ibatis.annotations.
Insert
;
import
org.apache.ibatis.annotations.
Select
;
import
org.springframework.stereotype.
Repository
;
import
javax.swing.text.Style;
import
java.util.List;
public interface
UserDao {
/**
* 新增用户
*
@param
user
*/
@Insert
(
"insert into user(id,username) VALUES(#{id},#{username})"
)
void
createUser(User user);
/**
* 查询用户列表
*
@return
*/
@Select
(
"select * from user"
)
List findAllUser();
}
---------------------------------------------------------------------------------------
xml version
="1.0"
encoding
="UTF-8"
?>
mapper
PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"
>
<
mapper
namespace
="com.spd.springbootdemo.dao.UserDao"
>
<
resultMap
id
="BaseResultMap"
type
="com.spd.springbootdemo.vo.User"
>
<
result
column
="id"
property
="id"
/>
<
result
column
="username"
property
="username"
/>
resultMap
>
<
parameterMap
id
="User"
type
="com.spd.springbootdemo.vo.User"
/>
<
sql
id
="Base_Column_List"
>
id, username
sql
>
<
select
id
="findAllUser"
resultMap
="BaseResultMap"
>
select
u.id,u.username
from user u
select
>
<
insert
id
="createUser"
parameterMap
="User"
useGeneratedKeys
="true"
keyProperty
="id"
>
insert into
user(id,username)
values
(#{id},#{username})
insert
>
mapper
>
测试:
源码:https://download.csdn.net/download/weixin_41228362/10410222