概述
First:第一种,简单生成
简单的实现,自己手动映射mapper
1:配置pom.xml文件进行导包
<!--spring mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2:写yml文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/depot
username: root
password: 12209
3:mapper类,一般可以自动生成,通过generatorconfig,下面介绍,这里不介绍了
@Service
// @Mapper 这里可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
@Select("SELECT * FROM tb_user WHERE id = #{id}")
User getUserById(Integer id);
@Select("SELECT * FROM tb_user")
public List<User> getUserList();
@Insert("insert into tb_user(username, age, ctm) values(#{username}, #{age}, now())")
public int add(User user);
@Update("UPDATE tb_user SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
public int update(@Param("id") Integer id, @Param("user") User user);
@Delete("DELETE from tb_user where id = #{id} ")
public int delete(Integer id);
}
4:写service,impl,control,user实体类
5:application需要配置一下
@MapperScan("com.example.mybatisdemo.mapper")//mapper方法的路路径
Second:第二种,增加mybatis的配置
配置文件版
更改上面的
1:yml加上mybatis的配置 增加的xml文件的地方
mybatis:
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
2:mapper.xml注意修改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">
<mapper namespace="com.example.mybatisdemo.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.example.mybatisdemo.bean.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="ctm" property="ctm" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List" >
id, username, age, ctm
</sql>
<select id="getUserList" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
</select>
<select id="getUserById" parameterType="java.lang.Integer" resultMap="BaseResultMap" >
SELECT
<include refid="Base_Column_List" />
FROM tb_user
WHERE id = #{id}
</select>
<insert id="add" parameterType="com.example.mybatisdemo.bean.User" >
INSERT INTO
tb_user
(username,age,ctm)
VALUES
(#{username}, #{age}, now())
</insert>
<update id="update" parameterType="java.util.Map" >
UPDATE
tb_user
SET
username = #{user.username},age = #{user.age}
WHERE
id = #{id}
</update>
<delete id="delete" parameterType="java.lang.Integer" >
DELETE FROM
tb_user
WHERE
id = #{id}
</delete>
</mapper>
3.现在是mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
</configuration>
4.更改mapper类了
User getUserById(Integer id);
源代码:https://download.csdn.net/download/qq_38905146/10551490
public List<User> getUserList();
public int add(User user);
public int update(@Param("id") Integer id, @Param("user") User user);
public int delete(Integer id);
第三种;自动生成mapper的文件:
加个映射文件,generator,然后配置yml文件
还要配置idea,配置pom文件
<!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>
https://blog.csdn.net/winter_chen001/article/details/77249029
当然还要配置,给你们找个博客吧
https://blog.csdn.net/winter_chen001/article/details/77249029
最后
以上就是可爱河马为你收集整理的pringboot + mybatis的全部内容,希望文章能够帮你解决pringboot + mybatis所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复