标签:class blog code java http ext
第一步:创建项目导入jar包
第二步:建表:
CREATE TABLE `person` ( `ID` int(10) NOT NULL AUTO_INCREMENT, `NAME` varchar(10) DEFAULT NULL, `GENDER` int(10) DEFAULT NULL, `ADRESS` varchar(50) DEFAULT NULL, `BIRTHDAY` date DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
第二步:建立实体类
package com.hao947.model; import java.util.Date; public class Person { private Integer id; private String name; private Integer gender; private String address; private Date birthday; @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", address=" + address + ", birthday=" + birthday + "]"; } }
第四步:创建配置文件
<?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> <typeAliases> <!-- 自定义别名: type:要定义的数据类型 alias:别名的名字 非自定义别名: 规则:别名jdk提供的所有的类的名字不区分大小写,如果是包装类那么直接使用其基本类型也可以 --> <typeAlias type="com.hao947.model.Person" alias="person"/> </typeAliases> <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" /> <property name="username" value="root" /> <property name="password" value="hao947" /> </dataSource> </environment> </environments> <!-- 集中管理库表的映射文件 --> <mappers> <mapper resource="com/hao947/sql/mapper/PersonMapper.xml" /> </mappers> </configuration>
第五步:库表的mapping映射文件
<?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"> <!-- namespace:当前库表映射文件的命名空间,唯一的不能重复 --> <mapper namespace="com.hao947.sql.mapper.PersonMapper"> <!-- id:当前sql的唯一标识 parameterType:输入参数的数据类型 resultType:返回值的数据类型 #{}:用来接受参数的,如果是传递一个参数#{id}内容任意,如果是多个参数就有一定的规则,采用的是预编译的形式select * from person p where p.id = ? ,安全性很高 --> <select id="selectPersonById" parameterType="java.lang.Integer" resultType="com.hao947.model.Person"> select * from person p where p.id = #{id} </select> </mapper>
第六步:测试代码
package com.hao947.test; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import com.hao947.model.Person; public class PersonTest { SqlSessionFactory sqlSessionFactory; @Before public void setUp() throws Exception { // 读取资源流 InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml"); // 初始化session工厂 sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } @Test public void selectPersonById() { // 创建一个sqlsession SqlSession session = sqlSessionFactory.openSession(); try { Person p = session.selectOne( "com.hao947.sql.mapper.PersonMapper.selectPersonById", 1); System.out.println(p); } finally { session.close(); } } }
标签:class blog code java http ext
原文地址:http://blog.csdn.net/hao947_hao947/article/details/34512871