1.mapper動態(tài)代理開發(fā)
1.1還是先在mybaits的配置文件中把mapper的xml配置好
sqlMapConfig.xml:
<mapper resource="mybaits/mapper/UserMapper.xml"/>
1.2 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">
<!--命名空間指定UserMapper,把UserMapper和UserMapper.xml綁定-->
<mapper namespace="mybaits.mapper.UserMapper">
<select id="findById" parameterType="Integer" resultType="mybaits.pojo.User">
SELECT * FROM user WHERE id = #{value}
</select>
</mapper>
1.3 UserMapper.java
package mybaits.mapper;
import mybaits.pojo.User;
//必須是接口
public interface UserMapper {
//方法名期奔、參數(shù)昭齐、返回值需要和Usermapper.xml的select的id浆熔、parameterType澄成、resultType一致
public User findById(Integer id);
}
1.4 調(diào)用
@Test
public void test() throws IOException {
//加載核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//創(chuàng)建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findById(32);
System.out.println(user);
}
其實區(qū)別就是之前的是使用sqlSession.select/insert(方法名,方法參數(shù))
現(xiàn)在是sqlSession.getMapper(XXXMapper.class),拿到接口的對象顾画,使用接口的對象調(diào)用方法。
2 mybaits配置文件的<mappers>
<mappers>主要是告訴mybaits酸舍,書寫了sql的xml文件在哪里。
- <mapper resource="mybaits/sqlmap/User.xml"/>
直接指定xml
- <mapper class="com.itheima.mybatis.mapper.UserMapper" />
指定java類里初,不過需要UserMapper.java和UserMapper.xml必須在同一個文件夾下,然后必須同名
- <package name="mybaits.sqlmap"/>
指定一個文件夾忽舟,也是要UserMapper.java和UserMapper.xml必須在同一個文件夾下双妨,然后必須同名
3 輸入類型為Pojo包裝類QueryVo
QueryVo也是一個pojo類,只是它內(nèi)部包含了其他的pojo類
比如根據(jù)User類的id查詢叮阅,User類是QueryVo的一個參數(shù)刁品,傳參的時候直接把QueryVo傳給mapper的xml,使用的時候直接user.id
QueryVo:
package mybaits.pojo;
public class QueryVo {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UserMapper.java
//必須是接口
public interface UserMapper {
//方法名浩姥、參數(shù)挑随、返回值需要和Usermapper.xml的select的id、parameterType勒叠、resultType一致
public User findById(Integer id);
//看這一句話
public User findByIdByQueryVo(QueryVo queryVo);
}
UserMapper.xml
<select id="findByIdByQueryVo" parameterType="mybaits.pojo.QueryVo" resultType="mybaits.pojo.User">
SELECT * FROM user WHERE id = #{user.id}
</select>
調(diào)用者:
@Test
public void test2() throws IOException {
//加載核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//創(chuàng)建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo = new QueryVo();
User user = new User();
user.setId(31);
user.setUsername("tom");
queryVo.setUser(user);
User backUser = mapper.findByIdByQueryVo(queryVo);
System.out.println(backUser);
}
4 resultMap :pojo類和數(shù)據(jù)庫表的字段不一致兜挨。
首先當pojo類和數(shù)據(jù)庫表的字段不一致時,依然使用resultType的話眯分,查詢到的數(shù)據(jù)中不一致的字段是無法被賦值的拌汇。
比如
可以看到字段userid不一樣,然后查詢到的結(jié)果是:
使用:resultMap
<mapper namespace="mybaits.mapper.OrderMapper">
<resultMap id="resultmap1" type="mybaits.pojo.Orders">
<!--column:數(shù)據(jù)庫的字段名噪舀,property:pojo類的字段名-->
<id column="id" property="id"/>
<id column="user_id" property="userId"/>
<id column="number" property="number"/>
</resultMap>
<!--resultMap="resultMap的id"-->
<select id="find" parameterType="mybaits.pojo.Orders" resultMap="resultmap1">
SELECT * FROM orders WHERE id = #{id}
</select>
</mapper>
查詢到的結(jié)果:
5 動態(tài)sql(復雜一點的sql)
5.1 if where
需求:根據(jù)性別和名字查詢。但是姓名和性別可能為空飘诗。
大概sql語句是:select * from user where id = id and username = username
但是呢与倡,如果id或者username為空的話,上面的sql語句就出錯了昆稿,所以需要if else 判空
mapper:
<!--<where>的作用是可以去掉第一個前and-->
<select id="findBySexAndName" parameterType="mybaits.pojo.User" resultType="mybaits.pojo.User">
SELECT * FROM user
<where>
<if test="id != null and id!= ''">
id = #{id}
</if>
<if test="username != null and username != ''">
AND username = #{username}
</if>
</where>
</select>
5.2 提取公共sql代碼(感覺沒啥用)
<sql id="sql1">
SELECT * FROM user
</sql>
<!--<where>的作用是可以去掉第一個前and-->
<select id="findBySexAndName" parameterType="mybaits.pojo.User" resultType="mybaits.pojo.User">
<!--引用sql的語句-->
<include refid="sql1"/>
<where>
<if test="id != null and id!= ''">
id = #{id}
</if>
<if test="username != null and username != ''">
AND username = #{username}
</if>
</where>
</select>
5.3 foreach
需求:根據(jù)多id查詢多個
sql語句:select * from user where id in (1,2,3)
這個(1,2,3)是數(shù)組傳進來的纺座,不一定有多少個
調(diào)用者:
@Test
public void test5() throws IOException {
//加載核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//創(chuàng)建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Integer[] ids = new Integer[3];
ids[0] = 10;
ids[1] = 16;
ids[2] = 22;
List<User> list = mapper.findByIds(ids);
for (User u : list) {
System.out.println(u);
}
}
mapper:
<!--雖然有數(shù)組參數(shù)傳進來,但是不需要parameterType-->
<select id="findByIds" resultType="mybaits.pojo.User">
SELECT * FROM user
<where>
id IN
<!--這里array代表數(shù)組溉潭,list代表集合-->
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
5.4 關(guān)聯(lián)查詢
需求:查詢多有的訂單order比驻,然后關(guān)聯(lián)到訂單的購買者User
一個User可以購買多個訂單该溯,一個訂單只屬于一個User
mapper:
<resultMap id="resultmap2" type="mybaits.pojo.Orders">
<!--result 和 id效果一樣,只是id是用來id的别惦,result是用來其他字段的-->
<!--關(guān)聯(lián)查詢需要給所有字段都映射一下狈茉,不映射的字段會是null-->
<id column="id" property="id"/>
<result column="number" property="number"/>
<result column="user_id" property="userId"/>
<result column="createtime" property="createtime"/>
<!--關(guān)聯(lián)User-->
<association property="user" javaType="mybaits.pojo.User">
<result column="username" property="username"/>
</association>
</resultMap>
<select id="selectOrder" resultMap="resultmap2">
SELECT o.id, o.user_id, o.number, o.createtime, u.username FROM orders o LEFT JOIN user u ON o.user_id = u.id
</select>
查詢到的結(jié)果:
Orders{id=3, userId=1, number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015, note='null', username=王五}
Orders{id=4, userId=1, number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015, note='null', username=王五}
Orders{id=5, userId=10, number='1000012', createtime=Thu Feb 12 16:13:23 CST 2015, note='null', username=張三}
6 mybaits逆向工程
根據(jù)數(shù)據(jù)庫表,自動生產(chǎn)pojo類
在導入工程掸掸,然后配置generatorConfig.xml氯庆,然后把生成的類復制過去
6 番外:mysql的大小寫
在Linux下:
數(shù)據(jù)庫名和表名是區(qū)分大小寫的
在Windows下:不區(qū)分