MyBatis学习笔记-HelloWorld

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();
		}
	}
}

总结

输入图片说明

转载于:https://my.oschina.net/Endless2010/blog/1483869

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值