Mybatis连接数据库

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>
    <properties resource="db.properties">      
    </properties>
    <environments default="development">
         <environment id="development">
             <transactionManager type="JDBC" />
             <dataSource type="POOLED">
                 <property name="driver" value="${jdbc.driver}" />
                 <property name="url" value="${jdbc.url}" />
                 <property name="username" value="${jdbc.username}" />
                 <property name="password" value="${jdbc.password}" />
             </dataSource>
         </environment>
    </environments>
    <mappers>
         <mapper resource="EmployeeMapper.xml" />
    </mappers>
</configuration>

1.1但是讷,这里配置全局配置文件之前,需要将数据的驱动(driver) ,路径(url),用户名(username),密码(password)。写入一个db.properties的文件中

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/java_study

jdbc.username=root

jdbc.password=root

1.2然后讷,需要将上面的db.properties文件的路径加载到全局配置文件中

<!--

         这里有个说明

         1.加载内路径使用resourse

         2.加载磁盘路径使用url

 -->

    <properties resource="db.properties"></properties>

1.3之后就是加载数据库啦

<environments default="development">

         <environment id="development">

             <transactionManager type="JDBC" />

             <dataSource type="POOLED">

                 <property name="driver" value="${jdbc.driver}" />

                 <property name="url" value="${jdbc.url}" />

                 <property name="username" value="${jdbc.username}" />

                 <property name="password" value="${jdbc.password}" />

             </dataSource>

         </environment>

</environments>

        2.然后是创建Dao层对应的Mapper文件咯

2.1首先讷,Dao层是这样的(这里省略了getter,setter,构造,以及equals,toString方法咯)

public class Employee {

    private Integer id;

    private String lastName;

    private String gender;

    private String email;

    

    public Employee() {

         super();

    }

}

 2.2然后讷,到然是创建Employee对应的Mapper配置文件啦

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

<!-- 强调一哈子,

         1.这里的namespace对应此Mapper的命名空间

         2.而且<select>标签中对应的id为此select语句的唯一标识符

         3.resultType属性对应的是查询结果的返回类型

         4.#{id}对应的传入参数

-->

<mapper namespace="com.atguigu.mybatis.EmployeeMapper">

  <select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee">

    select * from tbl_employee where id = #{id}

  </select>

</mapper>

  2.3最后当然是将Mapper配置文件引入全局配置文件(config.xml)中,嗯!就是这样的

<!--

         这里就是引入Mapper配置文件的地方

         resource属性对应Mapper文件的路径哈~

 -->

    <mappers>

         <mapper resource="EmployeeMapper.xml" />

    </mappers>

  3.你以为没了吗,那不存在的。其实到这就完了,这里只是测试一哈子

@Test

    public void test() throws IOException {

         //从xml中构建SqlSessionFactory实例

         String resource = "mybatis-config.xml";

         InputStream inputStream = Resources.getResourceAsStream(resource);

         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

         

         //从SqlSessionFactory中获取SqlSession对象

         SqlSession openSession = sqlSessionFactory.openSession();

         

         try {

         /**

          * 查询语句

          * 第一个参数对应Mapper文件中唯一标识符,对应哪一个sql语句

          * 第二个参数对应传入的参数

          */

             

             Employee employee = openSession.selectOne(

                     "com.atguigu.mybatis.EmployeeMapper.selectEmp", 1);

             

             System.out.println(employee);

         

         } catch (Exception e) {

             // TODO: handle exception

         //关闭Session咯,拜拜!

             openSession.close();

         }

3.1从xml中构建SqlSessionFactory实例

      

 String resource = "mybatis-config.xml";

         InputStream inputStream = Resources.getResourceAsStream(resource);

         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

3.2从SqlSessionFactory中获取SqlSession对象
     

   SqlSession openSession = sqlSessionFactory.openSession();

  3.3查询语句
       

  Employee employee = openSession.selectOne(

                     "com.atguigu.mybatis.EmployeeMapper.selectEmp", 1);

 4.测试结果

有图有真相

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值