Mybatis的注解开发1

2022-03-23 16:50:11 浏览数 (1)

使用注解的单表的CRUD操作

1.pom.xml 导入坐标

代码语言:javascript复制
<?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>org.example</groupId>
    <artifactId>Anno</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

2.编写pojo类

代码语言:javascript复制
private Integer id;
private String username;
private String address;
private String sex;

3.编写主配置文件

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- mybatis的主配置文件 -->
<configuration>
    <!--引入外部文件-->
    <!--引入数据库配置文件-->
    <properties resource="jdbcConfig.properties"></properties>
    <typeAliases>
        <!--配置别名-->
        <package name="com.rpf.domain"></package>
    </typeAliases>
    <!--配置环境-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--指定带有注解的dao接口所在位置-->
    <mappers>
        <package name="com.rpf.dao"></package>
    </mappers>
</configuration>

4.编写dao接口

代码语言:javascript复制
package com.rpf.dao;

import com.rpf.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 这是一个新建的类
 * 在Mybatis中针对CRUD有四个注解
 * 配置注解即不用写dao映射文件
 * @Select @Insert @Update @Delete
 */
public interface IUserDao {
    /**
     * 查询所有用户
     * @return
     */
    @Select("select * from user")//配置查询操作的注解
   List<User> findAll();

    /**
     * 保存操作
     * @param user
     */
    @Insert("insert into user(username,address,sex) values(#{username},#{address},#{sex})")
    void saveUser(User user);

    /**
     * 更新用户
     * @param user
     */
    @Update("update user set username=#{username},address=#{address},sex=#{sex} where id=#{id}")
    void updateUser(User user);

    /**
     * 删除用户
     * @param userId
     */
    @Delete("delete from user where id=#{id}")
    void deleteUser(Integer userId);

    /**
     * 查询一个
     * @param userId
     */
    @Select("select * from user where id=#{id}")
   User findById(Integer userId);

    /**
     * 根据名称查询 模糊查询
     */
    @Select("select * from user where username like #{username}")
    //第二种写法@Select("select * from user where username like '%${value}%' ")
    List<User> findByName(String userName);
}

0 人点赞