• SpringBoot_数据访问-整合MyBatis-注解版MyBatis


    第一步:配置druid数据源
    第二步 创建mapper类
    
    /**
     * Created by chengbx on 2018/6/29.
     */
    @Mapper
    public interface DepartmentMapper {
    
        @Select("select * from department where id =#{id}")
        public Department getDeptById(Integer id);
    
        @Delete("delete from department where id =#{id}")
        public int deleteById(Integer id);
    
        @Options(useGeneratedKeys = true,keyProperty = "id")
        @Insert("insert into department(department_name) values(#{departmentName}) ")
        public int insertDept(Department department);
    
        @Update("update department set department_name =#{departmentName} where id=#{id}")
        public int updateDept(Department department);
    }
    第三步 编写myBatis配置类 支持驼峰式命名
    
    @Configuration
    public class MyBatisConfig {
    
        @Bean
        public ConfigurationCustomizer configurationCustomizer(){
           return  new ConfigurationCustomizer(){
                /**
                 * Customize the given a {@link Configuration} object.
                 *
                 * @param configuration the configuration object to customize
                 */
                @Override
                public void customize(org.apache.ibatis.session.Configuration configuration) {
                    configuration.setMapUnderscoreToCamelCase(true);
                }
            };
        }
    }
    第四步 创建实体类bean
    public class Department {

    private Integer id;
    private String departmentName;

    public Department() {
    }

    public Department(Integer id, String departmentName) {
    this.id = id;
    this.departmentName = departmentName;
    }

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getDepartmentName() {
    return departmentName;
    }

    public void setDepartmentName(String departmentName) {
    this.departmentName = departmentName;
    }
    }

    public class Employee {

    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer did;

    public Employee() {
    }

    public Employee(Integer id, String lastName, Integer gender, String email, Integer did) {
    this.id = id;
    this.lastName = lastName;
    this.gender = gender;
    this.email = email;
    this.did = did;
    }

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public Integer getGender() {
    return gender;
    }

    public void setGender(Integer gender) {
    this.gender = gender;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public Integer getDid() {
    return did;
    }

    public void setDid(Integer did) {
    this.did = did;
    }
    }
    创建Controller类调用
    
    @RestController
    public class DepartmentController {
    
        @Autowired
        private DepartmentMapper departmentMapper;
    
        @GetMapping(value = "/dept/{id}")
        public Department getDepartment(@PathVariable("id") Integer id){
    
            return departmentMapper.getDeptById(id);
        }
    
        @GetMapping(value = "/dept")
        public Department insertDept(Department department){
    
             departmentMapper.insertDept(department);
            return department;
        }
    
    }
    pom.xml
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.baoxing</groupId>
    <artifactId>springboot-mybatis-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mybatis-01</name>
    <description>Demo project for Spring Boot</description>

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.15.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</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/com.alibaba/druid -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.8</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    <repositories>
    <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    </repository>
    </repositories>

    <pluginRepositories>
    <pluginRepository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </pluginRepository>
    <pluginRepository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    </pluginRepository>
    </pluginRepositories>


    </project>
  • 相关阅读:
    模拟登陆江西理工大学教务系统
    python3爬虫 -----华东交大校园新闻爬取与数据分析
    以selenium模拟登陆12306
    PAT (Basic Level) Practice (中文)1076 Wifi密码 (15 分)
    PAT (Basic Level) Practice (中文)1047 编程团体赛 (20 分)
    PAT (Basic Level) Practice (中文)1029 旧键盘 (20 分)
    PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
    PAT (Basic Level) Practice (中文)1031 查验身份证 (15 分)
    PAT (Basic Level) Practice (中文)1041 考试座位号 (15 分)
    PAT (Basic Level) Practice (中文)1037 在霍格沃茨找零钱 (20 分)
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9244114.html
Copyright © 2020-2023  润新知