发表于: 2019-11-02 22:03:13

1 949


今天完成的事情:学习Mybatis      

Mybatis
核心组件
SqlSessionFactoryBuilder
SqlSessionFactory
SqlSession(现在用SQL Mapper)
SQL Mapper

配置文件生成SqlSessionFactory和java代码生成
配置文件:基础配置文件(mybatis-config.xml:放在工程类路径下)映射关系的配置文件
<typeAlias>元素定义一个别名,它代表了一个类
<environment>:
<transactionManager>配置事务管理器
<dataSource>配置数据库(type="POOLED"代表采用内部提供的连接池方式 定义一些JDBC属性信息)
<mapper>引入映射器

SqlSessionFactory SqlSessionFactory =null;
    String resource = "mybatis-config.xml";
    InputStream inputStream;

try
   inputStream = Resource.getResourceAsStream(resource);
    SqlsessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
优点:不需要 重新编译代码 利于维护和修改
缺点:比如在配置文件中需要配置加密过的数据库用户名和密码,在生成前解密成明文


public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();

List<Student> st = session.selectList("listStudent");
for(Student s : st){
System.out.println(s.getName());
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ksy.pojo">
<select id="listStudent" resultType="Student">
select * from student
</select>
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.ksy.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/task1?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ksy/pojo/Student.xml"/>

                    
明天计划的事情:    任务18做任务 接着之前放弃的学习基础知识    整合spring和mybatis                 
遇到的问题: 学习框架有点慢 基础需要花点时间补一下                         
收获:

改BUG更熟练了 看的懂大部分的BUG原因了

会使用Mybatis对数据库进行增删改查

会改配置文件

大部分的问题自己解决居然没麻烦师兄 ,

被柯诗阳师兄安利了一个有许多案例的学习网站,有用


返回列表 返回列表
评论

    分享到