Mybatis入门案例

1.创建java项目,并导入相关开发包

2. 创建表

create database mydb;
use mydb;
create table user (
id int primary key auto_increment,
name varchar(255),
age int
);
insert into user values (null,'aaa',19),(null,'bbb',29),(null,'ccc',39),(null,'ddd',22),(null,'eee',33);

3.创建bean

package cn.wy.domain;

public class User {
    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4.编写配置文件sqlMapconfig.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>

    <!-- 配置数据源 -->
    <!--通过default可以切换当前使用的数据库-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>

        <environment id="oracle">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
                <property name="username" value="ht1602" />
                <property name="password" value="htdb" />
            </dataSource>
        </environment>
    </environments>


    <!-- 配置映射文件 -->
    <mappers>
        <mapper resource="UserMapper.xml" />
    </mappers>

</configuration>

5.编写映射文件,编写sql

namespace:命名空间,需要全世界唯一,一般采用公司名称反写

id:保证当前项目唯一

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">
<!--namespace名称空间,需要唯一,一般以公司名称反写-->
<mapper namespace="cn.wy.mapper.UserMapper">
	<select id="selec03" resultType="cn.wy.domain.User">
		select * from user where id=#{id};
	</select>
	<insert id="insert1">
		insert into user values (null ,#{name},#{age});
	</insert>
	<select id="selec01" resultType="cn.wy.domain.User">
		select * from user;
	</select>
	<select id="selec02" resultType="cn.wy.domain.User">
		select * from user where age between #{min} and #{max};
	</select>
</mapper>

6.将映射文件配置到sqlMapperConfig.xml

具体请查看步骤4

    <!-- 配置映射文件 -->
    <mappers>
        <mapper resource="UserMapper.xml" />
    </mappers>

7.测试类中添加测试方法测试


    @Test
    public void test01() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("slqMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder()
                .build(inputStream);
        SqlSession sqlSession = factory.openSession();
        List<Object> list = sqlSession.selectList("cn.wy.mapper.UserMapper.selec01");

        System.out.println("list:"+list);
        sqlSession.close();

    }

项目结构

测试结果

数据库数据

8.调用过程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值