Mybatis学习笔记
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <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.0</version> </dependency> @RestController public class LibrarianController { @Autowired private LibrarianService librarianService; @GetMapping("/getLibrarian") public Librarian getALibrarianInfo(int id) { //System.out.println("test :id: "+id); return librarianService.selectLibrarian(id); } } @Service public class LibrarianServiceImpl implements LibrarianService{ @Autowired private LibrarianMapper librarianMapper; @Override public Librarian selectLibrarian(int id) { // TODO Auto-generated method stub return librarianMapper.selectLibrarian(id); } } package com.example.dao; import org.apache.ibatis.annotations.Mapper; import com.example.entity.Librarian; @Mapper public interface LibrarianMapper { Librarian selectLibrarian(int id); }
我用了@Mapper是可以的,网上说也可用@Repository 一个mybatis的,一个 spring的
<?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.springbootdemo.dao.HelloMapper"> <select id="getUserNameById" parameterType="INTEGER" resultType="String"> select username from user where 1=1 and id = #{id,jdbcType=INTEGER} </select> </mapper>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mg?serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root mybatis.type-aliases-package=com.example.springbootdemo.pojo mybatis.mapperLocations=classpath:mapper/*.xml
踩坑记录:
1 记得需要 url选择时区, 为 UTC才可以,需要配置 pojo位置和 mapper的位置
出错有没有注释掉zookeeper,一直报错
2 mybatis 配置基本类型,可全大写,首字母大写,全小写都可以
3
he server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver
4 一般数据库连接 后缀
?serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull