第一次用idea 搭建mybatis

时间:2022-10-10 16:21:41

首先新建一个mavan 项目 选择Createfrom archetype  选择archetype-webapp 创建

在pom.xml文件里配置需要的包

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>

</dependencies>

创建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>

//配置文件

    <properties resource="db.properties"/>

//起别名    

<typeAliases>

        <typeAlias type="com.oracle.mapper.Role" alias="role"/>

    </typeAliases>

    <environments default="development">

        <environment id="development">

            <transactionManager type="JDBC"/>

dataSource标签

是配置数据源的链接信息 type属性是供我们对数据库连接方式的配置 

UNPOOLED 非数据库连接池 

POOLED 数据库连接池

JNDL 数据源

property标签是定义数据库的各类参数

            <dataSource type="POOLED">

                <property name="driver" value="${driver}"/>

                <property name="url" value="${url}"/>

                <property name="username" value="${username}"/>

                <property name="password" value="${password}"/>

            </dataSource>

        </environment>

    </environments>

3mapper标签是引入映射器

注解:<mapper class=“类的全限定名”/>

XML:<mapper resource=“包名/包名/xxx.xml>

    <mappers>

        <mapper resource="mapper/roleMapper.xml"/>

    </mappers>

</configuration>



resources创建数据库连接的配置文件db.properies 

driver=com.mysql.jdbc.Driver

username=root

password=53542316

url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&tcharacterEncoding=UTF-8



resources创建映射器mapper文件夹创建mapper.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="com.oracle.mapper.RoleMapper">

    <!-- type:映射实体类的数据类型 idresultMap的唯一标识 -->

    <resultMap type="com.oracle.mapper.Role" id="Map">

        <!-- column:库表的字段名 property:实体类里的属性名 -->

        <id column="id" property="id"/>

        <result column="rolename" property="rolename"/>

        <result column="note" property="note"/>

    </resultMap>

    <select id="selectAll" resultMap="Map">

        select * from t_role

    </select>

    <select id="getRole"

            parameterType="long"

            resultType="role">

        SELECT * FROM t_role WHERE id=#{id}

    </select>

    <insert id="addRole"

            parameterType="role" useGeneratedKeys="true" keyProperty="id">

        INSERT into t_role(rolename,note) VALUES(#{rolename},#{note})

    </insert>

    <select id="gRole" resultType="int" >

        SELECT COUNT(*)FROM t_role

    </select>

    

</mapper>



创建ManualExample

public class ManualExample {

    public static voidmain(String[] args) {

        //会话工厂

        SqlSessionFactory sessionFactory=null;

        //读取配置文件

        String resource ="mybatis-config.xml";

        InputStream inputStream =null;

        try {

           //从类路径中寻找文件

            inputStream = Resources.getResourceAsStream(resource);

        } catch (IOException e) {

            e.printStackTrace();

        }

        sessionFactory =newSqlSessionFactoryBuilder().build(inputStream);

        //从工厂中取一个会话, 类似于 Connection

        SqlSession session = sessionFactory.openSession();

        System.out.println(session);

       //声明接口 session生成一个我们看不见的实现类

        RoleMapper roleMapper = session.getMapper(RoleMapper.class);

        Role role = roleMapper.getRole(1);

        System.out.println(role.getRolename()); 

        session.commit();

        session.close();

    }

}

java包中创建接口类

public interface RoleMapper {

    //接口名字和刚才的mapper.xml定义的id要一致

    public Role getRole(longl);

    public void addRole(Role role);

    public int gRole();

    public List<Role> selectAll();

}