zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ASP字符串16进制互转
    LINQ Dictionary加速查询(TryGetValue)
    ASP中将UTF8格式的URL进行解码。函数
    C# 保护进程不被结束(源代码)防任务管理器结束进程
    用WebBrowser做web 打印时的权限不足。。。。
    .net 2.0 中 ‘注册为 allowDefinition='MachineToApplication' 的节是错误的’
    sql 2005 中分页
    BitComet web 插件和 flash get web 插件 对脚本的影响
    同时使用 C# and VB.NET 在 .net 2.0 中
    用触发器实现主从表关系(主表更改从表更改 )
  • 原文地址:https://www.cnblogs.com/genestart/p/11267548.html
Copyright © 2011-2022 走看看