标签:style use resources and 关系 bat images name utf-8
<?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/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com\sunyard\dao\xml\UserMapper.xml"/> </mappers> </configuration>
package com.sunyard.model; import java.util.Date; /** * Created by gaoeb on 2017/4/24. */ public class User { private int id; private String username; private String password; private int credits; private String lastIp; private Date lastVisit; private Date createTime; private Date updateTime; private int status; // getter and setter }
package com.sunyard.dao; import com.sunyard.model.User; /** * Created by gaoeb on 2017/4/24. */ public interface UserMapper { User getUserById(int id); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sunyard.dao.UserMapper"> <select id="getUserById" parameterType="java.lang.Integer" resultType="com.sunyard.model.User"> SELECT id AS id, username AS username, password AS password, credits AS credits, last_ip AS lastIp, last_visit AS lastVisit, create_time AS createTime, update_time AS updateTime, status AS status FROM t_user WHERE id = #{id} </select> </mapper>
package com.sunyard.util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; /** * Created by gaoeb on 2017/4/24. */ public class MyBatisUtil { private static SqlSessionFactory sqlSessionFactory = null; public static SqlSessionFactory getSessionFactory() { if (sqlSessionFactory == null) { try { sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); return sqlSessionFactory; } catch (IOException e) { e.printStackTrace(); } } return sqlSessionFactory; } }
import com.sunyard.dao.UserMapper; import com.sunyard.model.User; import com.sunyard.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); SqlSession session = MyBatisUtil.getSessionFactory().openSession(); UserMapper userMapper = session.getMapper(UserMapper.class); User user = userMapper.getUserById(1); System.out.println(user.toString()); session.close(); } }
标签:style use resources and 关系 bat images name utf-8
原文地址:http://www.cnblogs.com/geb515/p/6759904.html