一、MySql

引入包

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>8.0.26</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.mybatis.spring.boot</groupId>
  12. <artifactId>mybatis-spring-boot-starter</artifactId>
  13. <version>2.2.0</version>
  14. </dependency>

application.yml配置

  1. spring.datasource.url=jdbc:mysql://localhost:3306/test_spring?characterEncoding=utf8&useSSL=true
  2. spring.datasource.username=root
  3. # spring.datasource.password=1234
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

二、SpringBoot操作Mysql

0、准备工作

建表,初始数据

  1. create table user_test(
  2. id int not null,
  3. name varchar(20),
  4. sex int not null,
  5. cityid int not null
  6. );
  7. -- 放两条数据
  8. insert into user_test(id,name,sex,cityid) values(1,'cimon',1,1);
  9. insert into user_test(id,name,sex,cityid) values(2,'crb',1,122);

model类或者POJO类

  1. import lombok.Data;
  2. @Data
  3. public class User {
  4. private int id;
  5. private String name;
  6. private int sex;
  7. private int cityid;
  8. }

1、JdbcTemplate

参考:SpringBoot 使用jdbcTemplate操作数据库

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-jdbc</artifactId>
  4. </dependency>
  1. import com.cimon.model.User;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.jdbc.core.JdbcTemplate;
  4. import org.springframework.jdbc.core.RowMapper;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. public class UserDaoJdbcTemplateImpl implements UserDao {
  11. @Autowired
  12. private JdbcTemplate jdbcTemplate;
  13. @Override
  14. public Long insertUser(User user) {
  15. String sql="insert into user_test(id,name,sex,cityid) values(:id,:name,:sex,:cityid)";
  16. Map<String,Object> param = new HashMap();
  17. return null;
  18. }
  19. public User getUserByName(String name){
  20. String sql="select id,name,sex,cityid from user_test where name=?";
  21. User user = this.jdbcTemplate.queryForObject(sql, new Object[]{name}, new RowMapper<User>() {
  22. @Override
  23. public User mapRow(ResultSet resultSet, int i) throws SQLException {
  24. User u = new User();
  25. u.setId(resultSet.getInt("id"));
  26. u.setCityid(resultSet.getInt("cityid"));
  27. u.setName(resultSet.getString("name"));
  28. u.setSex(resultSet.getInt("sex"));
  29. return u;
  30. }
  31. });
  32. return user;
  33. }
  34. public List<User> getUserByNameList(String name){
  35. return null;
  36. }
  37. }

测试代码:

  1. import com.cimon.dao.UserDao;
  2. import com.cimon.model.User;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @SpringBootTest
  9. @RunWith(SpringRunner.class)
  10. public class UserDaoJdbcTemplateImplTest {
  11. @Autowired
  12. private UserDao userDao;
  13. @Test
  14. public void test(){
  15. User user = userDao.getUserByName("crb");
  16. System.out.println(user.toString());
  17. }
  18. }

2、JPA

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-jpa</artifactId>
  4. </dependency>

省略,见参考的博客

3、MyBatis

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>2.2.0</version>
  5. </dependency>

注意:没有spring前缀

  1. #mybatis.config-locations=classpath:mybatis/mybatis-config.xml
  2. mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
  3. mybatis.type-aliases-package=com.cimon.dao
  1. package com.cimon.mapper;
  2. import com.cimon.model.User;
  3. public interface UserMapper {
  4. // Long insertUser(User user);
  5. User getUserByName(String name);
  6. }
  1. package com.cimon;
  2. import com.cimon.mapper.UserMapper;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringRunner;
  9. @SpringBootTest
  10. @RunWith(SpringRunner.class)
  11. @MapperScan("com.cimon.mapper")
  12. public class MyBatisMapperTest {
  13. @Autowired
  14. private UserMapper userMapper;
  15. @Test
  16. public void test(){
  17. System.out.println(userMapper.getUserByName("cimon"));
  18. }
  19. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cimon.mapper.UserMapper">
  4. <resultMap id="userMap" type="com.cimon.model.User">
  5. <result column="id" property="id" jdbcType="INTEGER"/>
  6. <result column="name" property="name" jdbcType="VARCHAR"/>
  7. <result column="sex" property="sex" jdbcType="INTEGER"/>
  8. <result column="cityid" property="cityid" jdbcType="INTEGER"/>
  9. </resultMap>
  10. <sql id="base_column">
  11. id,name,sex,cityid
  12. </sql>
  13. <select id="getUserByName" resultMap="userMap">
  14. select
  15. <include refid="base_column"/>
  16. from user_test
  17. where name=#{name}
  18. </select>
  19. <!-- <insert id="insertUser" parameterType="com.cimon.model.User">-->
  20. <!-- insert <include refid="base_column"/>-->
  21. <!-- values-->
  22. <!-- (#{d},#{name},#{sex},#{cityid})-->
  23. <!-- </insert>-->
  24. </mapper>

mapper文件名要和mapper类名一致
image.png

参考

1、Spring Boot实战:数据库操作- 3种方法
2、SpringBoot 使用jdbcTemplate操作数据库
3、元素类型 “mapper“ 声明属性 “namespace“解决
4、mybatis XML 映射配置
5、springboot中使用mybatis
6、mybatis的两种使用方式
7、mybatis标签
8、MyBatis中的resultType