JavaEE學習日誌持續更新----> 必看!JavaEE學習路線(文章總彙)
MyBatis
數據源配置
獲取連接的時間:等到真正要執行sql語句時,纔會獲得一個連接。
environments屬性:
<!--數據庫的環境
default:指定默認的環境
-->
<environments default="development">
<!--id:環境唯一的標誌-->
<environment id="development">
<!--事務管理:jdbc事務-->
<transactionManager type="JDBC"/>
<!--
dataSource:數據源(數據庫連接池)配置
type="POOLED":數據源的類型配置
POOLED:使用mybatis的自帶數據源配置
UNPOOLED:不使用數據源配置,使用Connection操作數據庫
JNDI:JNDI服務數據源配置
-->
<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>
事務管理
默認爲事務開啓
SqlSession sqlSession = sqlSessionFactory.openSession();
想要將事務關閉,傳遞true
SqlSession sqlSession = sqlSessionFactory.openSession(true);
注意:事務在結束時,必須把事務關閉(mybatis中自動關閉事務)
@Test
public void testSave(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//獲取Dao接口的動態代理對象
UserDao dao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setUsername("大大明");
user.setPassword("123");
user.setAddress("濟南");
user.setSex("女");
user.setBirthday("1999-2-22");
dao.save(user);
sqlSession.close();
}
動態sql語句
靜態sql語句的缺陷
若條件中只給出一個條件,則會報錯。
<select id="findByCondition" resultType="user" parameterType="user">
select * from user where username like "%"#{username}"%" and sex = #{sex}
</select>
public class TestFrame {
@Test
public void test(){
//獲取sqlsession對象
SqlSession sqlSession = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("mybatis-config.xml")).openSession();
//獲取動態代理對象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//執行方法
User user = new User();
user.setUsername("明");
List<User> userList = userDao.findByCondition(user);
for (User u : userList) {
System.out.println(u);
}
//關閉資源
sqlSession.close();
}
}
if和where標籤
解決方式一:使用<if>
標籤處理
<!--多條件查詢-->
<select id="findByCondition" resultType="user" parameterType="user">
select * from user where 1=1
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</select>
解決方式二:使用<where>
標籤處理
注意:前面也需要加上and
<!--多條件查詢
where:可以處理第一個and,後面的and不能處理
所以多條件都要寫上and關鍵字
-->
<select id="findByCondition" resultType="user" parameterType="user">
select * from user
<where>
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</where>
</select>
sql片段的重複使用
重複使用sql語句:使用<sql>
和<include>
標籤
<sql id="select_user">
select * from user
</sql>
<select id="findByCondition" resultType="user" parameterType="user">
/*關聯使用sql片段*/
<include refid="select_user"></include>
<where>
<if test="username != null">
and username like "%"#{username}"%"
</if>
<if test="sex != null">
and sex = #{sex}
</if>
</where>
</select>
foreach標籤
循環標籤<foreach>
- collection:list或array
- open:前綴
- close:後綴
- separator:分隔符
- item:循環的每一個對象
- index:循環索引
原始的sql語句
delete from user where id in (7,9)
foreach執行sql
<delete id="delByArray" parameterType="int[]">
delete from user where
<foreach collection="array" open="id in (" close=")" separator="," item="" index="id">
#{id}
</foreach>
</delete>