mybatis开发使用过程

实体类接口的映射文件

<?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="com.dao.studentdao">
<!--    id与接口对应的唯一id,执行sql语句唯一表示,mybatis用此标识去执行sql语句-->
<!--    resultType执行sql语句返回数据类型  书写类的全类名-->
    <select id="selectstudent" resultType="com.domin.student">
    select name,age,email from student order by id
  </select>
<!--    还有增删改查-->

</mapper>

mybatis核心配置文件

<?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">

<!-- mybatis的核心配置文件 -->
<configuration>
	<!-- 配置日志的输出方式 -->
	<settings>
		<setting name="logImpl" value="LOG4J" />
	</settings>
	<!-- 别名优化 -->
	<typeAliases>
		<package name="com.sixmai.domain"/>
	</typeAliases>
<!--	环境配置 数据路链接信息-->
<!--	默认-->
	<environments default="mydf">
<!--		一个数据库的配置信息 id代表一个环境的信息及时环境的名称-->
		<environment id="">
<!--			JDBC驱动连接-->
			<transactionManager type="JDBC"></transactionManager>
<!--			POOLED表示使用连接池-->
			<dataSource type="POOLED">
<!--				数据库信息-->
				<property name="" value=""/>
				<property name="" value=""/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com\dao\studentdao.xml"/>
	</mappers>

</configuration>

---三层架构mvc
---框架界面springmvc 逻辑层spring 数据库连接层mybatis
---mybati功能:访问数据库增删改查
步骤:1.加入依赖maven
2创建到层 定义操作数据库方法  接口形式
3.创建mapper文件 即使sql语句映射文件**.xml 与接口一一对应的sql语句(唯一表示ID)
4.创建主文件mybatis。con.xml  ---连接数据库 ---指定mapper位置
5.mybatis的放法sqlsession执行语句

测试`在这里插入代码片


public class test {
    public static void main(String[] args) throws IOException {
        String config = "mybatis.cfg.xml";
        InputStream in = Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        String sqlid = "路径"+"sqlectstudents";
        List<student> students = sqlSession.selectList(sqlid);
//        students.forEach(student -> System.out.println(students));
        for (student s : students){
            System.out.println(s);
        }
        sqlSession.close();



    }
}

在这里插入图片描述
参考代码


public class test {
    public static void main(String[] args) throws IOException {
        String config = "mybatis.cfg.xml";
//        负责读取主配置文件的类
        InputStream in = Resources.getResourceAsStream(config);
//        构建SqlSessionFactory对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sqlSessionFactory = builder.build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        String sqlid = "路径"+"sqlectstudents";
//        查寻
//        List<student> students = sqlSession.selectList(sqlid);
        students.forEach(student -> System.out.println(students));
//        for (student s : students){
//            System.out.println(s);
//        }
//        插入
        student student1 = new student();
        student1.getAge();
        student1.getEmail();
        student1.getName();
        int num = sqlSession.insert(sqlid,student1);
        
        
        sqlSession.close();



    }
}

参考https://www.bilibili.com/video/BV185411s7Ry?p=48

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值