Mybatis
马克-to-win:为什么现在大量的公司倾向于用Mabatis,而不用hibernate?我认为很重要一点的原因是Mabatis能自动生成类文件和配置文件,节省了大量的工作量。当然还有很多其他原因。
但底下援引网上一些意见关于mybatis的缺点,技术总监选型做项目时一定要慎重。mybatis缓存使用不当,容易产生脏数据。mybatis的sql写在xml里,可读性很低,调试也非常困难,也非常受限,无法像jdbc那样在代码里根据逻辑实现复杂动态sql拼接。mybatis不如直接用spring提供的jdbc简单框架(Template),同样支持对象映射。
Mybatis中helloworld例子
helloworld例子:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203
(目录结构和需要导的包都见图)
做一个普通的java project:
例 1.1
package com;
public class Register {
public String toString() {
return "id:"+id+"\nname:"+name+"\nage:"+age;
}
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name ;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.mapper;
import com.Register;
public interface RegisterMapper {
Register selectByPrimaryKey(Integer id);
}
RegisterMapper.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.mapper.RegisterMapper">
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.Register">
select Id, Name, Age from register
where Id = #{id}
</select>
</mapper>
mybatis-config.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>
<environments default="myEnv">
<environment id="myEnv">
<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="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/RegisterMapper.xml" />
</mappers>
</configuration>
Mybatis可以配置适应多种环境。例如:开发,测试和生产环境需要不同的配置;随你怎么命名,只要保证默认环境ID要匹配其中一个环境ID。
<transactionManager type="JDBC"/>中可设两值:JDBC或MANAFED来说明事务模型。二者的不同之处在于:前者是直接使用JDK提供的JDBC来管理事务的各个环节:提交、回滚、关闭等操作,而后者交由容器来管理事务的整个生命周期(比如 Spring 或 JEE 应用服务器的上下文),当有事务的实验时,同学们可看到他们的区别。本例看不出二者的区别。
import java.io.IOException;
import java.io.InputStream;
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 com.Register;
import com.mapper.RegisterMapper;
public class TestMark_to_win {
private SqlSession sqlSession = null;
private RegisterMapper registerMapper = null;
public static void main(String[] args) {
TestMark_to_win tmw = new TestMark_to_win();
tmw.processData();
}
public void processData() {
sqlSession = getSession();
registerMapper = sqlSession.getMapper(RegisterMapper.class);
dataSelect();
if (null != sqlSession) {
sqlSession.commit();
sqlSession.close();
}
}
private void dataSelect() {
Register register = registerMapper.selectByPrimaryKey(1);
System.out.println(register.toString());
}
private SqlSession getSession() {
try {
InputStream is = Resources
.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(is);
return sqlSessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
运行结果是:
id:1
name:张三
age:13