实体类接口的映射文件
<?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();
}
}