MyBatis学习笔记-HelloWorld
MyBatis学习笔记-HelloWorld
Endless2010 发表于5个月前
MyBatis学习笔记-HelloWorld
  • 发表于 5个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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

总结

输入图片说明

共有 人打赏支持
粉丝 2
博文 25
码字总数 23027
×
Endless2010
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: