MyBatis学习笔记-HelloWorld
2017-07-23 00:00
393 查看
MyBatis配置文件
mybatis-config.xml<properties resource>元素可以指定properties文件位置,导入里面配置的值
<typeAlias>定义了一些别名,如student,用来代替全名com..Student
<mapper>元素配置mapper.xml的位置
<?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="com/endless/mybatis/helloworld/config/jdbc.properties" /> <typeAliases> <typeAlias alias="student" type="com.endless.mybatis.helloworld.po.Student"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/endless/mybatis/helloworld/config/student-mapper.xml" /> </mappers> </configuration>
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/school username=root password=1234
mapper配置文件
student-mapper.xml这个文件里面配置sql,namespace为对于DAO接口
<select>定义了一条select语句,id对应上面namespace定义接口里的方法,parameterType和resultType分别对应该方法的参数和返回类型
<?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="com.endless.mybatis.helloworld.mapper.StudentMapper"> <!-- student是在mybatis-config.xml中配置的Alias --> <select id="getStudent" parameterType="String" resultType="student"> select * from student where id=#{studentId} </select> </mapper>
Mapper接口
public interface StudentMapper { public Student getStudent(String studentId); } //这里Student的属性名称对和数据库的字段一致,会自动填充到Student对象中返回 public class Student { private int id; private String name; private int age; private String gender; //省略get,set }
测试程序
public class MyBatisTest { public static void main(String[] args){ String resource="com/endless/mybatis/helloworld/config/mybatis-config.xml"; SqlSession sqlSession=null; try{ //SqlSessionFactoryBuilder读取配置文件创建SqlSessionFactory对象 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource)); //sqlSessionFactory对象用来创建session,相当于JDBC的Connection对象 sqlSession=sessionFactory.openSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); Student student=studentMapper.getStudent("10001"); System.out.println(student); }catch(Exception e){ e.printStackTrace(); }finally{ if(sqlSession!=null) sqlSession.close(); } } }
总结
相关文章推荐
- mybatis学习笔记(一) 项目搭建,helloworld实例
- 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件
- MyBatis学习笔记四(MyBatis多表查询)
- mybatis学习笔记
- mybatis3.2.7学习笔记1_Mapper编程
- Spring学习笔记之HelloWorld
- [struts2学习笔记] 第三节 创建struts 2 HelloWorld所需的六个步骤
- mybatis学习笔记(5)-SqlMapConfig
- 字段名与实体类属性不匹配([尚硅谷]_张晓飞_Mybatis 学习笔记二)
- JavaWeb学习笔记-mybatis-09-mybatis配置-properties
- Objective-C 学习笔记:从HelloWorld开始
- Mybatis学习笔记——动态Sql
- ActiveMQ学习笔记之HelloWorld
- [maven学习笔记]第一节,认识maven,搭建maven开发环境,写第一个HelloWorld
- U3D学习笔记1: HelloWorld
- MyBatis增删改示例——MyBatis学习笔记之二
- mybatis学习笔记(3)-入门程序二
- mybatis学习笔记(4)-开发dao方法
- 1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)
- Android(OPhone) 学习笔记 - HelloWorld资源