Mybatis详细学习总结七大点——(一)基础配置

基础配置(掌握)

第一步:导入maven依赖

<dependencies>
    <dependency>
      <groupId>org.jectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.4</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.44</version>
    </dependency>
  </dependencies>
 
  <build>
    <finalName>springDemo1</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

第二步:新建mybatis-config.xml文件,并导入配置文件

<?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">
<configuration>
    <!--配置db.properties-->
    <properties resource="db.properties"></properties>
    <!--配置bean层别名-->
    <typeAliases>
        <package name="com.xudan.mybatis.bean"/>
    </typeAliases>
    <!--配置数据库环境-->
    <environments default="mysql1">
        <environment id="mysql1">
            <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>
    <!--配置mapper映射器-->
    <mappers>
        <package name="com.xudan.mybatis.dao"/>
    </mappers>
</configuration>

第三步:建一个实体类

/**
 * 用户实体类
 */
public class Users {
    private Integer id;
    private String account;
    private String password;
    private String name;
    private String phone;
    private Date birthday;
    private String gender;
    private String address;
    public Users(){

    }

    public Users(String account, String password, String name, String phone, Date birthday, String gender, String address) {
        this.account = account;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.birthday = birthday;
        this.gender = gender;
        this.address = address;
    }

    public Users(Integer id, String account, String password, String name, String phone, Date birthday, String gender, String address) {
        this.id = id;
        this.account = account;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.birthday = birthday;
        this.gender = gender;
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

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

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", birthday=" + birthday +
                ", gender='" + gender + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

第四步:建一个接口

/**
 * user的dao接口
 * 建议:
 *  1、dao接口和mapper.xml同包
 *  2、dao接口和mapper.xml同名
 */
public interface IUserDao {
    /**
     * 查询所有用户信息
     * @return
     * @throws Exception
     * 注意:如果是使用了默认的mybatis设置,方法的参数列表长度只能是0或者1
     */
    public List<User> findUsers() throws Exception;
    /**
     * 根据用户编号修改用户信息
     * @param user
     * @return
     * @throws Exception
     */
    public int updateUsers(User user)throws Exception;
}

第五步:建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="userMapper">
<!--   1.查询所有用户信息
        id : 标识符
        resultType : 返回的结果类型-->
    <select id="findAll" resultType="User">
        select * from users
    </select>
    <!--根据id查询用户信息-->
    <select id="findUserById" resultType="User" parameterType="java.lang.Integer">
        select * from users where id=#{id}
    </select>
    <!--2. 新增一个用户信息
            parameterType : 参数类型
            mybatis提供了一种占位符的写法: #{实际参数}-->
    <insert id="addUser" parameterType="User">
        insert into users(account,password) value(#{account},#{password})
    </insert>
    <!--3.修改一个用户信息-->
    <update id="updateUser" parameterType="User">
        update users set account=#{account},password=#{password} where id=#{id}
    </update>
    <!--4. 删除一个用户信息-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from users where id=#{id}
    </delete>
</mapper>

第六步:简单测试

public class UserTest {
    private SqlSession session;
    private IUserDao userDao;
    @Before
    public void init() throws IOException {
        //获取文件
        InputStream resource= Resources.getResourceAsStream("SqlMapperConfig.xml");
        //创建SqlSessionFactory
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(resource);
        //创建session,操作statement
        session=factory.openSession(true);
        //创建IUserDao
        userDao=session.getMapper(IUserDao.class);
    }
    @Test
    public void findUsers(){
        //查询数据
        try {
            System.out.println(userDao.findUsers());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据用户编号,修改用户信息
     */
    @Test
    public void updateUsers(){
        User user=new User();
        user.setUserId(1);
        user.setAccount("yuyang1");
        try {
            System.out.println(userDao.updateUsers(user));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @After
    public void destroy(){
        //关闭session,释放资源
        session.close();
    }
}

如此便做了一个简单的mybatis的测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值