标签:style http io os ar 使用 java sp 文件
mybatis是一个ORM框架,因为activiti使用了mybatis,所以学习下;
mybatis单独运行可以不依靠任何框架,也不需要tomcat;
一,步骤概要:
1,手动创建数据库表,创建一个USER表;
2,创建核心配置文件configuration.xml;
3,创建POJO实体文件:user.java;
4,创建DAO:接口usermapper.java,接口实现usermapper.xml;
5,创建测试类;
二,具体步骤:
总览项目文件目录:
1,创建USER表(使用H2数据库,嵌入式数据库,便于测试);
//创建USER表 CREATE TABLE USER ( ID INT IDENTITY ( 1,1 ) PRIMARY KEY , USERNAME VARCHAR ( 10 ) ) ; //插入初始用户 INSERT INTO USER(USERNAME ) VALUES ( ‘用户一‘); INSERT INTO USER(USERNAME ) VALUES ( ‘用户二‘); INSERT INTO USER(USERNAME ) VALUES ( ‘用户三‘);
A,连接H2数据库,JAR包已经包含驱动:
B,创建出来的USER表:
2,configuration.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="db.properties" /> <typeAliases> <!--给实体类起一个别名 user ,别名便于在映射文件中代替全路径名,简化书写--> <typeAlias type="test.java.pojo.User" alias="User" /> </typeAliases> <!--数据源配置 这块用 H2数据库 连接信息在db.properties--> <environments default="development"> <environment id="development"> <transactionManager type="jdbc" /> <dataSource type="POOLED"> <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <mappers> <!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 --> <mapper resource="test/java/dao/UserMapper.xml" /> </mappers> </configuration>
3,创建user.java:
package test.java.pojo; 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; } }
4,创建DAO(增删改查)以及DAO实现
DAO接口:增删改查的接口,贯彻面向接口编程的思想,resultMap是与数据库字段对应的结果映射,可以简便查询结果取值
package test.java.dao; import java.util.List; public interface UserMapper { //普通的增删该查 public User findById(int id); public User findByName(String username); public List<User> findAll(); public int insert(User user); public int delete(String userName); public int update(User user); public int countAll(); }
DAO实现:mybatis实现方法,在XML输入SQL语句
<?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"> <!--这块等于dao接口的实现 namespace必须和接口的类路径一样--> <mapper namespace="test.java.dao.UserMapper"> <!-- findById必须和接口中的方法名一样 返回一个User 就是刚才的别名 如果不弄别名要连类路径一起写 麻烦--> <select id="findById" parameterType="HashMap" resultType="User"> select * from user where id=#{id} </select> <select id="findByName" parameterType="String" resultType="User"> select * from user where username=#{username} </select> <select id="findAll" resultMap="userResultMap"> select * from user </select> <resultMap type="User" id="userResultMap" > <id property="id" column="id"/> <result property="username" column="username"/> </resultMap> <insert id="insert" parameterType="User"> INSERT INTO USER VALUES (#{id}, #{username}) </insert> <delete id="delete" parameterType="String" > delete from user where userName like #{username} </delete> <update id="update" parameterType="User"> update user set userName=#{username} where id=#{id} </update> <select id="countAll" resultType="int"> select count(1) from user </select> </mapper>
5,测试类
package test.java.mytest; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.ibatis.annotations.ResultMap; 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 test.java.dao.UserMapper; import test.java.pojo.User; public class MybatisTest { public static void main(String[] args) { SqlSession sqlSession = getSessionFactory().openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(userMapper.countAll()); User user=new User(); user.setId(2); user.setUsername("文字7"); // userMapper.insert(user); userMapper.update(user); System.out.println(userMapper.countAll()); // System.out.println(userMapper.findByName(user.getUsername()).getId()); // System.out.println(userMapper.findAll().get(1).getUsername()); // List<User> user=userMapper.findAll(); // Iterator<User> it=user.iterator(); // while (it.hasNext()) { // User users=it.next(); // System.out.println(users); // // } // User user = userMapper.findById(2); // System.out.println(user.getUsername()); sqlSession.commit(); sqlSession.close(); } //会话工厂 private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "configuration.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sessionFactory; } }
三,总结
mybatis类似一种图示关系运行:
标签:style http io os ar 使用 java sp 文件
原文地址:http://my.oschina.net/acitiviti/blog/333280