?
以前的iBatis 项目在2010 年5 月,将代码库迁致Google Code,并更名为MyBatis。
本示例用的是ibatis-3-core-3.0.0.208.zip,首先加如mybatis和mysql数据库的jar包。
?
table.sql
?
create database mybatis; use mybatis; create table user( id int primary key auto_increment, name varchar(50) ); insert into user values(null,'张三');?
?
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> <typeAliases> <typeAlias alias="User" type="com.nantian.mybatis.model.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://127.0.0.1:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/nantian/mybatis/model/UserMapper.xml"/> </mappers> </configuration>?
?
DBUtil
?
package com.nantian.mybatis.data; 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 final DBUtil dbUtil = new DBUtil(); private SqlSessionFactory sqlSessionFactory = null; private DBUtil() { try { String resource = "com/nantian/mybatis/data/configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public static DBUtil getInstance() { return dbUtil; } public SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }?
?
User
?
package com.nantian.mybatis.model; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }?
?
UserMapper
?
package com.nantian.mybatis.model; public interface UserMapper { public User selectUser(int id); }?
?
?
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.nantian.mybatis.model.UserMapper"> <select id="selectUser" parameterType="int" resultType="User"> select id,name from user where id = #{id} </select> </mapper>?
?
Test
?
package com.nantian.mybatis.test; import junit.framework.TestCase; import org.apache.ibatis.session.SqlSession; import com.nantian.mybatis.data.DBUtil; import com.nantian.mybatis.model.User; import com.nantian.mybatis.model.UserMapper; public class Test extends TestCase { public void selectUser() { SqlSession sqlSession = DBUtil.getInstance().getSqlSessionFactory() .openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.selectUser(1); System.out.println(user.getName()); } finally { sqlSession.close(); } } }?
?
Mybatis3可以使用注解了。注解是非常简单明了的,但是JAVA 注解既有局限性,在语句比较复杂的情况下又比较容易混乱。所以,如果你的语句比较复杂,最好还是使用XML 来映射语句。这主要取决于你和你的项目团队,决定哪个更适合于你,主要还是以稳健为主。也就是说,你不需要制约于哪一种方式,你可以很容易的把注解转为XML,也可以把XML 转化为注解。
?
?
?
?
整合Mybatis与Spring3
http://www.iteye.com/topic/1018134
?
1.实体bean?
- package?org.hyn.bean;??
- ??
- public?class?User?{??
- ????private?int?id;??
- ????private?String?name;??
- ????private?int?age;??
- ...//长长的set?get?方法??
2.Mapper及配置文件
- package?org.hyn.maper;??
- ??
- import?org.hyn.bean.User;??
- ??
- public?interface?UserMapper?{??
- ????public?void?insertUser(User?user);??
- ??
- ????public?User?getUser(String?name);??
- }??
?
- <?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="org.hyn.maper.UserMapper">??
- ????<insert?id="insertUser"?parameterType="org.hyn.bean.User">??
- ????????insert?into?user(name,age)?values(#{name},#{age})???
- ????????</insert>??
- ????<select?id="getUser"?resultType="org.hyn.bean.User"??
- ????????parameterType="java.lang.String">??
- ????????select?*?from?user?where?name=#{name}???
- ???????</select>??
- ??
- ????<!--?当使用该Mybatis与Spring整合的时候,该文件必须和相应的Mapper接口文件同名,并在同一路径下?-->??
- </mapper>???
3.spring配置文件
- <bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">??
- ????<property?name="dataSource"?ref="myDataSource"?/>??
- ??
- ????<!--?<property?name="configLocation"?value=""/>?-->??
- ????<!--??
- ????????该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置 ?
- ????????。当然如果你不打算添加typeAlias?之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了??
- ????-->??
- ??
- ????<!--<property?name="mapperLocations"?value="src/UserMapper.xml"/>-->??
- ????<!--?该配置文件用来指定Mapper映射文件的位置?,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->??
- </bean>??
- ??
- <!-- ?注册Mapper方式一?
- <bean?id="userMapper"?class="org.mybatis.spring.mapper.MapperFactoryBean">?
- ? ? <property name="mapperInterface"?value="org.hyn.maper.UserMapper"/>?
- ? ? <property?name="sqlSessionFactory"?ref="sqlSessionFactory"/>?
- </bean> ?
- -->??
- ??
- <!--?注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper?,配置如下:-->??
- <bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">??
- ????<property?name="basePackage"?value="org.hyn.maper"?/>??
- </bean>??
4.测试类
- @Test??
- public?void?testGetUser()?{??
- ????ApplicationContext?aContext?=?new?FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");??
- ????UserMapper?userMapper?=?aContext.getBean(UserMapper.class);??
- ????User?user?=?userMapper.getUser("张三");??
- ????System.out.println(user.toString());??
- }??
- ??
- @Test??
- public?void?testAddUser(){??
- ????ApplicationContext?aContext?=?new?FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");??
- ????UserMapper?userMapper?=?aContext.getBean(UserMapper.class);??
- ????User?user?=?new?User();??
- ????user.setName("张三");??
- ????user.setAge(18);??
- ????userMapper.insertUser(user);??
- ????System.out.println("添加成功");??
- }??
?
?
?
?
下面付上源代码
?
?
?
mybatis mapper pojo的两种方式
?
1.如果数据库和pojo的属性名都一样的话可以用如下方式:
<select id="getUser" resultType="org.guarx.domain.User" parameterType="java.lang.String"> <![CDATA[select * from users where id=#{value}]]> </select> <select id="getUsers" resultType="org.guarx.domain.User"> <![CDATA[select * from users]]> </select>?
?
2.如果数据库columns name和pojo的属性名称不一样(大多数情况下)
(1)第一种方式:
?
<select id="getUsers" resultType="org.guarx.domain.User"> <![CDATA[select t_id as id,t_username as name,t_desc as description from users]]> </select>?
?
(2)第二种方式:
?
<resultMap type="org.guarx.domain.User" id="userMapper"> <result column="t_id" property="id"/> <result column="t_username" property="username"/> <result column="t_desc" property="description"/> </resultMap> <select id="getAll" resultMap="userMapper"> <![CDATA[select id,username,description from users ]]> </select>?
?
?
?
?
?
?
?
?
?