在Hibernete过后,再次记录一下自己对另一款ORM框架的学习记录。
首先导入相关的jar包
简单的一些文件目录
首先 这是 sqlMapConfig.xml 配置文件的内容,这是Mybatis的主配置文件
<?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 type="com.wzh.pojo.User" alias="User" />
<typeAlias type="com.wzh.pojo.Person" alias="Person" />
<typeAlias type="com.wzh.pojo.Order" alias="Order" />
</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/springmvc" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="com/wzh/pojo/User.xml" />
<mapper resource="com/wzh/pojo/Person.xml" />
<mapper resource="com/wzh/pojo/Order.xml" />
</mappers>
</configuration>
1、首先定义了 要使用的实体类的别名 在实体的映射文件中避免了多出要写类全限定名字的苦恼
2、配置一些数据库连接的信息
3、引入映射的文件
接着是 映射文件 (与hibernate不同的是 Mybatis需要自己写sql 语句)
这是User类
package com.wzh.pojo;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "id :"+this.id+" name :"+this.name;
}
}
这是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.wzh.pojo.User">
<!-- 查询User -->
<select id="selectUserByID" parameterType="long" resultType="com.wzh.pojo.User">
select * from user where id = #{id}
</select>
<!-- 添加User -->
<insert id="insertUser" parameterType="com.wzh.pojo.User">
insert user(name) values (#{name})
</insert>
<!-- 删除 -->
<delete id="deleteUserByID" parameterType="string">
delete from user where id = #{id}
</delete>
<!-- 更新 -->
<update id="updateUser">
update user set name = #{name} where id = #{id}
</update>
<!-- 加条件查询 -->
<select id="selectUserByConditions" parameterType="hashmap" resultType="com.wzh.pojo.User">
select * from user
<where>
<if test="id != null">
id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
</where>
</select>
</mapper>
写出了简单的增删改查,以及加条件查询的时候如何利用mybatis生成动态的sql
如何使用?
//数据库相关操作的sqlsession
SqlSession sqlSession ;
@Before
public void initSF() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void testSelect() throws Exception {
User user = (User) sqlSession.selectOne("com.wzh.pojo.User.selectUserByID", "1");
System.out.println(user);
}
@Test
public void testAdd() throws Exception {
User user = new User();
user.setName("程序插入2");
int i = sqlSession.insert("com.wzh.pojo.User.insertUser",user);
//不提交的话可能会出问题
sqlSession.commit();
System.out.println(i);
}
@Test
public void testDelete() throws Exception {
int i = sqlSession.delete("com.wzh.pojo.User.deleteUserByID", "1");
System.out.println(i);
}
@Test
public void testUpdate() throws Exception {
User user = new User();
user.setId(2l);
user.setName("修改后。。");
int i=sqlSession.update("com.wzh.pojo.User.updateUser", user);
System.out.println(i);
}
@Test
public void testSelectByConditions() throws Exception {
Map map = new HashMap();
//map.put("id", 3l);
List<User> users =sqlSession.selectList("com.wzh.pojo.User.selectUserByConditions", map);
if(users.size()>0){
for(User temp : users){
System.out.println(temp);
}
}else{
System.out.println("没有查询到数据");
}
}
上面的就是操作方法
级联操作
Person 一对多 Order
package com.wzh.pojo;
import java.util.List;
public class Person {
private long personId;
private String name;
private List<Order> orders;
public long getPersonId() {
return personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
package com.wzh.pojo;
public class Order {
private long orderId;
private String orderName;
private Person person;
public long getOrderId() {
return orderId;
}
public void setOrderId(long orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
下面是映射文件
Person.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.wzh.pojo.Person">
<resultMap type="Person" id="personMap">
<id column="personId" property="personId"/>
<result column="name" property="name"/>
<!-- 一对多 -->
<collection property="orders" ofType="Order">
<id column="orderId" property="orderId"/>
<result column="name" property="orderName"/>
</collection>
</resultMap>
<!-- 关联查询 -->
<select id="selectPersonById" parameterType="string" resultMap="personMap">
select p.personId,p.name,o.orderId,o.name from person p,`order` o where p.personId = o.pid and p.personId = #{id}
</select>
</mapper>
Order.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.wzh.pojo.Order">
<resultMap type="Order" id="orderMap">
<id column="orderId" property="orderId"/>
<result column="name" property="orderName"/>
<association property="person" javaType="Person">
<id column="personId" property="personId"/>
<result column="name" property="name"/>
</association>
</resultMap>
<select id="selectOrderByID" parameterType="string" resultMap="orderMap">
select p.personId,p.name,o.orderId,o.name from person p,`order` o where p.personId = o.pid and o.orderId = #{id}
</select>
</mapper>
以上两个的返回类型都设置成了 resultMap
最后我们可以级联查询
@Test
public void testSelectPerosnByID() throws Exception {
Person p =sqlSession.selectOne("com.wzh.pojo.Person.selectPersonById","1");
if(p!=null){
System.out.println("name:"+p.getName());
if(p.getOrders().size()>0){
for(Order order :p.getOrders()){
System.out.println("id"+order.getOrderId()+"name"+order.getOrderName());
System.out.println("==============================================");
}
}else{
System.out.println("没有获取到关联的订单或者无订单数据");
}
}
}
@Test
public void testSelectOrderByID() throws Exception {
Order order = sqlSession.selectOne("com.wzh.pojo.Order.selectOrderByID", "1");
System.out.println(order.getOrderName());
}