Mybatis简介大家可以自行google之。官网为:http://code.google.com/p/mybatis/
我也刚刚开始初学,先来一个小例子:
创建一个User.java文件,代码如下:
package com.rollen; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Name:"+name+"Age"+age; } }
上面的java文件位于com.rollen包中,然后在这个包中建立generator.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.rollen.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/user_db" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/rollen/user.xml" /> </mappers> </configuration>
在com.rollen包下面创建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="com.rollen.User"> <select id="selectUser" parameterType="int" resultType="User" > select * from user where age = #{age} </select> </mapper>
我们最后编写一个测试类,代码如下“:
package com.rollen; import java.io.*; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void main(String[] args) { String resource="com/rollen/generatorConfig.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = factory.openSession(); try { User user=sqlSession.selectOne("selectUser", 10); System.out.println(user); }finally{ sqlSession.close(); } } }
在上面的generateConfig.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> <property name="username" value="root" /> </properties> <typeAliases> <typeAlias alias="User" type="com.rollen.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/user_db" /> <property name="username" value="${username}" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/rollen/user.xml" /> </mappers> </configuration>
注意,和上面的区别是我们添加了:
<properties> <property name="username" value="root" /> </properties>
并且修改了这行代码:
<property name="username" value="${username}" />
还可以有如下的修改方法,新建一个config.properties文件,文件内容为:
password=root
然后将generatorconfig.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="com/rollen/config.properties"> <property name="username" value="root" /> </properties> <typeAliases> <typeAlias alias="User" type="com.rollen.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/user_db" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/rollen/user.xml" /> </mappers> </configuration>
需要注意的是如下的两行代码:
<properties resource="com/rollen/config.properties"> .... <property name="password" value="${password}" />