先来一个传送们哈(官方文档)
配置文件
<?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">
</properties>
<typeAliases>
<package name="com.suse.yuxin"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="sqlmap/User.xml"/> -->
<package name="com.suse.yuxin"/>
</mappers>
</configuration>
- 配置文件一般是放到classpath下面就扩以
Mapper文件
一般使用的是Mapper代理的形式来开发Dao,so mapper文件和接口文件放到一起
<?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.suse.yuxin.UserMapper">
<cache type=""/>
<select id="findUserById" parameterType="int" resultType="com.suse.yuxin.User">
SELECT * FROM user WHERE user_id=#{id}
</select>
<select id="findUserByUserName" parameterType="String" resultType="com.suse.yuxin.User">
SELECT * FROM user WHERE user_name LIKE '%${value}%'
</select>
<insert id="insertUser" parameterType="com.suse.yuxin.User">
<selectKey keyProperty="user_id" order="AFTER" resultType="int">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user(user_name,user_number) VALUES (#{user_name},#{user_number})
</insert>
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE user_id=#{value}
</delete>
<update id="updateUser" parameterType="com.suse.yuxin.User">
UPDATE user set user_name = #{user_name},user_number = #{user_number} WHERE user_id=#{user_id}
</update>
<select id="selectUser" parameterType="com.suse.yuxin.User" resultType="com.suse.yuxin.User">
SELECT * FROM user
<where>
<include refid="userSql"/>
</where>
</select>
<sql id="userSql">
<if test="user_name != null">
and user_name = #{user_name}
</if>
</sql>
<select id="findOrder" parameterType="int" resultMap="orderMap">
SELECT
`user`.user_id user_id,
`user`.user_name user_name,
`user`.user_number user_number,
`order`.order_id order_id,
`order`.order_content order_content
FROM `user`,`order` WHERE `user`.user_id = #{value} AND `order`.user_id = #{value}
</select>
<resultMap type="Order" id="orderMap">
<id column="order_id" property="order_id"/>
<result column="order_content" property="order_content"/>
<association property="user" javaType="com.suse.yuxin.User">
<id column="user_id" property="user_id"/>
<result column="user_name" property="user_name"/>
<result column="user_number" property="user_number"/>
</association>
</resultMap>
<select id="findUser" parameterType="int" resultMap="userMap">
SELECT
`user`.user_id user_id,
`user`.user_name user_name,
`user`.user_number user_number,
`order`.order_id order_id,
`order`.order_content order_content
FROM `user`,`order` WHERE `user`.user_id = #{value} AND `order`.user_id = #{value}
</select>
<resultMap type="User" id="userMap">
<id column="user_id" property="user_id"/>
<result column="user_name" property="user_name"/>
<result column="user_number" property="user_number"/>
<collection property="orders" ofType="com.suse.yuxin.Order">
<result column="order_content" property="order_content"/>
</collection>
</resultMap>
</mapper>
dao接口文件
仅仅是实现了部分
package com.suse.yuxin;
import java.util.List;
import java.util.Map;
public interface UserMapper {
User findUserById(int id);
User selectUser(User user);
List<Order> findOrder(int id);
User findUser(int id);
}
java操作数据库
package com.suse.yuxin;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
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 Demo {
public static void main(String args[]) throws IOException{
String configPath = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(configPath);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = factory.openSession();
/*User user = sqlSession.selectOne("User.findUserById", 1);
System.out.println("User "+user);*/
/*List<Object> users = sqlSession.selectList("User.findUserByUserName", "dapi");
System.out.println(users);*/
/*User user = new User();
user.setUser_name("yudapi");
user.setUser_number("12101010320");
int insert = sqlSession.insert("User.insertUser",user);
sqlSession.commit();
System.out.println("insert "+user);*/
/*int delete = sqlSession.delete("User.deleteUserById", 1);
System.out.println("delete " +delete);
sqlSession.commit();*/
/*User user = new User();
user.setUser_id(2);
user.setUser_name("name");
user.setUser_number("number");
int update = sqlSession.update("User.updateUser", user);
System.out.println("update "+update);
sqlSession.commit();*/
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(2);
System.out.println("user "+user);
//user.setUser_name(null);
userMapper.selectUser(user);
List<Order> order = userMapper.findOrder(2);
System.out.println("order "+order);
User user2 = userMapper.findUser(2);
System.out.println("user2 "+user2);
}
}
关于二级缓存
- mybatis主要是使用cache接口来实现二级缓存
- 对应的主流缓存都有对应的实现
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day