mybatis入门(二)--简单入门例子
下面介绍一个最基本的Mybatis入门程序(java project ---oracle数据库)。
????? 1.首先在数据库中创建表create table t_user(id number(11),username varchar2(20));
????? 2.Eclipse---New--JavaProject,导入相关jar包。(自己测试,随便建了个javaproject工程)
? ? ? 在src下面分别添加vo、maps、dao以及util包。
????? 3.在根目录下建立文件mybatis.cfg.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="config.properties"></properties><!-- end properties --><typeAliases><typeAlias alias="User" type="com.mybatis.test.vo.User" /></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/mybatis/test/maps/User.xml" /></mappers></configuration>?
?
?
?上面代码段中出现
<!-- 引入外部文件 --><properties resource="config.properties"></properties><!-- end properties --?
在根目录下新建文件config.properties,具体内容如下:
driver=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@10.45.218.152:1431:SZSTAGEusername=rootpassword=root
????? 4.在vo包下面新建使实体类User.java,具体内容如下:
package com.mybatis.test.vo;public class User {private int id;private String userName;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}@Overridepublic String toString(){return "id is:"+id+" name is: "+ userName;}}
?5.在maps包下新建xml文件User.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="User"> <select id="selectUser" parameterType="int" resultType="User">select * from t_user where id = #{id} </select> <select id="selectAllUser" resultType="User"> select id, username from t_user </select> <insert id="insertUser" parameterType="User"> insert into t_user(id,username) values (#{id}, #{userName}) </insert></mapper>
?6.在util包中新建辅助类DBUtil,具体内容如下:
package com.mybatis.test.util;import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class DBUtil {private static String resource = "mybatis.cfg.xml";private static Reader reader;private static SqlSessionFactory sqlSessionFactory;public static SqlSessionFactory getSqlSessionFactory(){try {reader = Resources.getResourceAsReader(resource);} catch (IOException e) {System.out.println("IO异常");}sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);return sqlSessionFactory;}}
?7.在dao包中新建类UserDAO,具体代码如下:
package com.mybatis.test.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import com.mybatis.test.util.DBUtil;import com.mybatis.test.vo.User;public class UserDAO {private static SqlSessionFactory sqlSessionFactory = DBUtil.getSqlSessionFactory();/** * 通过id获得User * * @param id * @return */public User getUserById(int id) {SqlSession session = sqlSessionFactory.openSession();try {User user = (User) session.selectOne("User.selectUser", id);return user;} finally {session.close();}}public List<User> getAllUser() {SqlSession session = sqlSessionFactory.openSession();try {List<User> users = (List<User>) session.selectList("User.selectAllUser");return users;} finally {session.close();}}public int addUser(User user) {SqlSession session = sqlSessionFactory.openSession();try {int i = session.insert("User.insertUser", user);if(i >= 1) session.commit();return i;} finally {session.close();}}
?8.在util包中新建Test类,进行测试,具体内容如下:
package com.mybatis.test.util;import java.util.List;import com.mybatis.test.dao.UserDAO;import com.mybatis.test.vo.User;public class Test {public static void main(String[] args) {UserDAO userDao = new UserDAO();User user = new User();user.setId(4);user.setUserName("Zhuzi");//添加System.out.println(userDao.addUser(user));//查询System.out.println(userDao.getUserById(4));List<User> users = userDao.getAllUser();for (User u : users) {System.out.println(u);}}}?
?????