标签:映射文件 new 对象 org nod url builder github 语句
转载请注明出处:http://www.cnblogs.com/yidaijiankuanzhongbuhui/p/7003993.html
用mybatis一年多了,一直是别人搭好框架,配置好各种配置文件和导入jar包,然后自己知道怎么用!回过头来再重新整理学习mybatis,发现很多事如果自己亲手操作会更好!
mybatis 官方文档上对mybatis定义是:mybatis是一流的持久层框架,支持自定义sql、存储过程和高级映射!它消除了几乎所有的JDBC代码、手工设置参数和结果的检索!mybatis可以使用简单的XML或注释进行配置 映射文件、映射接口和Java pojo(普通的旧Java对象)到数据库记录。
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/test1"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlMapper/chengjiMapper.xml"/> </mappers> </configuration>
首先需要在数据库test1中添加一个表,创建表的sql如下
CREATE TABLE `chengji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(11) DEFAULT NULL,
`xueke` varchar(255) DEFAULT NULL,
`fenshu` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
然后需要创建一个实体类:
public class chengji { private Integer id; private String name; private String xueke; private Integer fenshu; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getXueke() { return xueke; } public void setXueke(String xueke) { this.xueke = xueke; } public Integer getFenshu() { return fenshu; } public void setFenshu(Integer fenshu) { this.fenshu = fenshu; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", xueke=" + xueke + ", fenshu=" + fenshu + "]"; } }
创建基于xml的映射语句文件:
<?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="sqlmapper.chengjiMapper" > <resultMap id="BaseResultMap" type="model.chengji" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="xueke" property="xueke" jdbcType="VARCHAR" /> <result column="fenshu" property="fenshu" jdbcType="INTEGER" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" > select * from chengji where id = #{id,jdbcType=INTEGER} </select> </mapper>
我们下面可以写一个test.java 来测试一下:
public class test { public static void main(String[] args) { SqlSession session = getSqlSessionFactory().openSession(); try { String resource = "conf/mybatis_config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); chengji cj = session.selectOne("sqlmapper.chengjiMapper.selectByPrimaryKey", 4); System.out.println(cj.toString()); } finally { session.close(); } } } }
最后的打印结果是:chengji [id=4, name=gaoshan, xueke=yuwen, fenshu=99]
本文是作者原创,转载请标明
标签:映射文件 new 对象 org nod url builder github 语句
原文地址:http://www.cnblogs.com/yidaijiankuanzhongbuhui/p/7003993.html