MyBatis框架

发布时间:2018年12月13日作者:yafei浏览次数:905

2.3 创建MyBatis的全局配置文件

  • 参考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”>

<configuration>

         <!– 数据库连接环境的配置 –>

         <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/mybatis_1129” />

                                     <property name=“username” value=“root” />

                                     <property name=“password” value=“1234” />

                            </dataSource>

                   </environment>

         </environments>

         <!– 引入SQL映射文件,Mapper映射文件 –>

         <mappers>

                   <mapper resource=“EmployeeMapper.xml” />

         </mappers>

</configuration>

 

2.4 创建Mybatis的sql映射文件

  • 参考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”>

 

<mapper namespace=“suibian”>

         <select id=“selectEmployee” resultType=“com.atguigu.myabtis.helloWorld.Employee”>

                   select id ,last_name lastName ,email ,gender from tbl_employee where id = #{id}

                   <!– select * from tbl_employee  where id = #{id} –>

         </select>

</mapper>

 

2.5 测试

  • 参考MyBatis的官方手册

@Test

         public void test() throws Exception {

                   String resource = “mybatis-config.xml”;

                   InputStream inputStream = Resources.getResourceAsStream(resource);

                   SqlSessionFactory sqlSessionFactory =

                                               new SqlSessionFactoryBuilder().build(inputStream);

                   System.out.println(sqlSessionFactory);

 

                   SqlSession session  = sqlSessionFactory.openSession();

                   try {

                            Employee employee =

                                               session.selectOne(“suibian.selectEmployee”, 1001);

                            System.out.println(employee);

                   } finally {

                            session.close();

                   }

       }

 

 


上一篇:
下一篇:
相关课程

java培训 大数据培训 前端培训 UI/UE设计培训

关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园2号楼3层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)

西安市高新区和发智能大厦(西安校区)