其他分享
首页 > 其他分享> > mybatis01-mybatis创建步骤

mybatis01-mybatis创建步骤

作者:互联网

  1. 添加maven依赖
 <!--导入Mybatis依赖-->
    <dependencies>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  1. 创建配置文件
<?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>
    <!-- 配置 mybatis 的环境 -->
    <environments default="database">
        <!-- 配置 mysql 的环境 -->
        <environment id="database">
            <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置连接数据库的信息:用的是数据源(连接池) -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test1?serverTimezone=UTC"/>
                <property name="username" value="sa"/>
                <property name="password" value="jk200326"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 告知 mybatis 映射配置的位置 -->
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

<?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">
<!--namespace绑定一个dao/mapper接口-->
<mapper namespace="com.zzl.dao.IUserDao">
    <select id="getUserList" resultType="com.zzl.domain.User">
      select * from user
    </select>
</mapper>
  1. 类的创建
    private int useid;
    private String usename;
    private String password;

注意:实体类属性与数据表字段名一致

     //查询所有
     List<User> getUserList() ;
  1. 项目结构

标签:xml,实体类,配置文件,步骤,private,mysql,mybatis,mybatis01
来源: https://www.cnblogs.com/SuSVIP/p/15783963.html