myBatis 基于动态代理方式实现的增删改查

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>
    <environments default="development">
        <environment id="development">
            <!-- 事务提交方式:
                    JDBC:利用jdbc方式处理事务(commit rollback close)
                    MANAGED:将事务交由其他组件去托管(spring,jBoss),默认会关闭连接
                    -->
            <transactionManager type="MANAGED"/>
            <!-- 数据源类型:
                    POOLED:使用数据库连接池
                    UNPOOLED:传统的jdbc模式
                    JNDI:从tomcat中获取一个内置的数据库连接池
             -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/firstdb?serverTimezone=GMT"/>
                <property name="username" value="root"/>
                <property name="password" value="mysqlpasw"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="yang/text/mapper/personMapper.xml"/>
    </mappers>
</configuration>

2.配置映射文件

<?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="yang.text.mapper.personMapper">

    <select id="queryPersonById" resultType="yang.text.entity.Person" parameterType="int">
    select * from person where id = #{id}
      </select>

    <insert id="addPerson" parameterType="yang.text.entity.Person">
        insert into person(id,name,age) values(#{id},#{name},#{age})
    </insert>

    <update id="updatePerson" parameterType="yang.text.entity.Person">
        update person set id = #{id},name = #{name},language = #{age}
    </update>

    <delete id="deletePersonById" parameterType="int">
       delete from person where id = #{id}
    </delete>
</mapper>

3.配置映射文件对应的接口

package yang.text.mapper;

import yang.text.entity.Person;

public interface personMapper {
    Person queryPersonById(int id);
    void addPerson(Person person);
    void updatePerson(Person person);
    void deletePersonById(int id);
}

4.测试类

package yang.text.entity;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import yang.text.mapper.personMapper;

import java.io.Reader;

public class myBatisTest {
    public static void main(String[] args) throws Exception{
        Reader reader = Resources.getResourceAsReader("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);//可以通过build的第二个参数来改变数据库环境
        SqlSession sqlSession = sqlSessionFactory.openSession();
      personMapper mapper = sqlSession.getMapper(personMapper.class);
        Person person = mapper.queryPersonById(1);
        System.out.println(person.toString());
        sqlSession.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值