mybatis--第一个mybatis程序
首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下:
create database my; use my; create table user( id int(10) auto_increment, name varchar(64), level varchar(256), phone varchar(256), primary key(id) ); insert into user(id,name,level,phone) values(1,'a','a','1234555666');
项目目录如下:
其次,创建mybatis配置文件SqlMapConfig.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> <typeAliases> <typeAlias alias="User" type="com.zk.pojo.user" /> </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/my"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- // power by http://www.yiibai.com --> <mapper resource="com/zk/pojo/UserMapper.xml" /> </mappers> </configuration>
再次,创建实体类user.java和映射文件UserMapper.xml
user.java
package com.zk.pojo; public class user { private Integer id; private String name; private String level; private String phone; 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 getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "user [id=" + id + ", name=" + name + ", level=" + level + ", phone=" + phone + "]"; } }
UserMapper.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="com.zk.pojo.UserMapper"> <select id="GetUserByID" parameterType="int" resultType="user"> select * from user where id = #{id} </select> </mapper>
最后,写出实现类Helloword.java
package com.zk.pojo; import java.io.Reader; 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 com.zk.pojo.*; /** * * @author yiibai * @copyright http://www.yiibai.com * @date 2015/09/22 */ public class HelloWord { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SqlSession session = sqlSessionFactory.openSession(); try { user user = (user) session.selectOne("com.zk.pojo.UserMapper.GetUserByID", 1); if(user!=null){ String userInfo =user.getName()+", 所属部门:"+user.getLevel()+", 手机号:"+user.getPhone(); System.out.println(userInfo); } } finally { session.close(); } } }
运行Helloword.java