EurekaClient整合mybatis

在EurekaClient篇中已经创建好了项目,接着就是将mybatis整合进来

创建user表:

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

随便添加一条数据

 

项目结构如下:

 

 

 

首先需要在pom文件中添加引用jar包

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency> 

更新maven

User文件代码如下

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class User implements Serializable {
    /**
     */
    private Integer id;

    /**
     */
    private String username;

    /**
     */
    private String password;
}

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="com.szyin.userapi.mapper.UserRepository">
  <!-- CodeBuilder Generated -->
  <resultMap id="UserMap" type="com.szyin.userapi.dto.User">
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>

  <sql id="Base_List">
    id,username,password
  </sql>

  <select id="getList" resultMap="UserMap">
    select
    <include refid="Base_List"></include>
    from user
  </select>

</mapper>

UserRepository:

@Mapper
public interface UserRepository {

    List<User> getList();
}

UserServiceImpl:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> getList() {
        return userRepository.getList();
    }
}

UserService:

public interface UserService{

    List<User> getList();
}

UserAdminController:

@RestController
@RequestMapping(value = "/user")
public class UserAdminController {
    @Resource
    private UserService userService;

    @GetMapping(value = "/getList")
    public List<User> getList() {
        List<User> list = userService.getList();
        System.out.println(list);
        return list;
    }

}

最后在application.properties文件添加mysql项目的配置

server.port=10001
spring.application.name=client-user
#注册的eureka.Service信息
eureka.client.service-url.default-zone=http://127.0.0.8761/eureka/
eureka.instance.prefer-ip-address=true

#################mysql#################
#指定时区和编码
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

################mapper文件位置################
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

 

启动项目,http://localhost:10001/user/getList

 

posted @ 2020-06-03 17:17  音Duang  阅读(237)  评论(0编辑  收藏  举报