~~~
<!-- sql demo-->
CREATE TABLE `employee` (
`id` varchar(50) NOT NULL default '',
`name` varchar(50) default NULL,
`gender` char(1) default NULL,
`address` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `employee` VALUES ('1', '張全蛋', '1', '暴走大事件');
INSERT INTO `employee` VALUES ('2', '王尼瑪', '1', '暴走大事件');
INSERT INTO `employee` VALUES ('3', '李小華', '0', '富士康');
INSERT INTO `employee` VALUES ('4', '趙鐵柱', '1', '富士康');
INSERT INTO `employee` VALUES ('5', '風清揚', '1', '華山');
INSERT INTO `employee` VALUES ('6', '周杰倫', '1', '臺北');
INSERT INTO `employee` VALUES ('7', '卡死了', '1', '我說的');
~~~
> 使用步驟:
> 1) 導入jar包

> 2)添加mybatis全局配置文件,log4j日志文件,數據源信息文件
##### 1.新建一個文件夾(命名為resource或者config),將此文件夾放在類路徑下(怎么放?見下面步驟)


//全局配置文件
~~~
<?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>
<!-- 導入配置文件。可以在當前頁面 ${鍵} 來或者 鍵所對應的valueu值 key=value -->
<properties resource="dbconfig.properties"/>
<!-- 聲明別名 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}" />
<property name="username" value="${user}"/>
<property name="password" value="${psw}"/>
</dataSource>
</environment>
</environments>
<!-- 對應的實體類的配置文件 -->
<mappers>
<mapper resource="cn/li/pojo/EmployeeMapper.xml"/>
<mapper class="cn.li.dao.EmpDao"/>
</mappers>
</configuration>
~~~
//log4j日志文件
~~~
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="GBK" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
~~~
//數據源信息文件
~~~
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis1?characterEncoding=utf-8
user=root
psw=root
~~~
> 3)創建實例類(POJO,domain,javaBean,entity),實體類的映射文件(sql映射文件)
~~~
public class Employee {
private int id ;
private String name;
private char gender;
private String address;
//setter、getter方法 toString方法
}
~~~
~~~
//Mapper.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">
<!-- 每一個實體類 都對應著Mybatis的一個命名空間 。 namespace屬性值 相當于id,如果存在多個 不允許重復 -->
<!-- 命名空間,一般情況下,在當前空間中聲明的是,實體類對應的別名 -->
<mapper namespace="cn.li.dao.EmployeeDao">
<select id="selOne" resultType="cn.li.pojo.Employee">
select * from employee where
id=#{id}
</select>
<!-- 新增 刪除 修改的時候parameterType="cn.li.pojo.Employee -->
<!-- 新增員工 使用主鍵自增 自增必須有這兩個屬性useGeneratedKeys="true" keyProperty="id" -->
<insert id="insertEmp" useGeneratedKeys="true" keyProperty="id">
insert into employee(name,gender,address)
values(#{name},#{gender},#{address})
</insert>
<!-- 刪除員工 -->
<delete id="deleteEmp">
delete from employee where id=#{id}
</delete>
<!-- 更新員工 -->
<delete id="updateEmp">
update employee set name=#{name},gender=#{gender}
where id=#{id}
</delete>
<!-- 通過名字和性別查詢員工信息 -->
<select id="selEmpByNameAndGender" resultType="cn.li.pojo.Employee">
select * from
employee where name=#{name} and gender=#{gender};
</select>
<!-- 通過名字和性別查詢員工信息 入參是map集合 -->
<select id="selEmpByMap" resultType="cn.li.pojo.Employee">
select * from employee where
name=#{aa} and gender=#{bb};
</select>
<!-- 查詢所有員工 -->
<select id="selAllEmp" resultType="cn.li.pojo.Employee">
select * from employee
</select>
<!-- 動態sql 之if -->
<select id="selDS1" resultType="cn.li.pojo.Employee">
select * from employee where 1=1
<if test="name!=null">
and name like '%${name}%'
</if>
</select>
<!-- 動態sql 之choose when otherwise 注意 choose只選擇一次。如果所有when都不滿足的時候,otherwise中的內容才會被拼接 -->
<select id="selDS2" resultType="cn.li.pojo.Employee">
select * from employee where 1=1
<choose>
<when test="name!=null">
and name like '%${name}%'
</when>
<when test="address!=null">
and address='${address}'
</when>
<otherwise>
order by name
</otherwise>
</choose>
</select>
<!-- 動態sql 之where<> -->
<select id="selDS3" resultType="cn.li.pojo.Employee">
select * from employee
<where>
<choose>
<when test="name!=null">
name like '%${name}%'
</when>
<when test="address!=null">
address='${address}'
</when>
<otherwise>
order by name
</otherwise>
</choose>
</where>
</select>
<!-- 動態sql 之where<> and if -->
<select id="selDS4" resultType="cn.li.pojo.Employee">
select * from employee
<where>
<if test="name!=null">
name like '%${name}%'
</if>
<if test="address!=null">
and address='${address}'
</if>
</where>
</select>
<!-- 動態sql 之trim and if -->
<select id="selDS5" resultType="cn.li.pojo.Employee">
select * from employee
<trim prefix="WHERE" prefixOverrides="and /or ">
<if test="name!=null">
name like '%${name}%'
</if>
<if test="address!=null">
and address=#{address}
</if>
</trim>
</select>
<!-- 動態sql 之set -->
<update id="selDS6">
update employee
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="address!=null">
address=#{address}
</if>
</set>
where id=#{id}
</update>
<!-- 動態sql 之set -->
<update id="selDS7">
update employee
<trim prefix="SET" suffixOverrides=",">
<if test="name!=null">
name=#{name},
</if>
<if test="address!=null">
address=#{address}
</if>
</trim>
where id=#{id}
</update>
<!-- foreach批量刪除 -->
<delete id="deleteBatch">
delete from employee where id in
<!-- collection list 數組:array -->
<foreach collection="list" open="(" separator="," close=")"
item="ids">
#{ids}
</foreach>
</delete>
</mapper>
~~~
> 4)將映射文件注冊到mybatis全局配置文件中

> 5)編寫SQL,根據全局配置文件拿到SqlSessionFactory,通過SqlSessionFactory拿到SqlSession對象,調用封裝好的方法進行CRUD,關閉會話。
~~~
try {
// 根據全局配置文件。利用SqlSessionFactoryBuilder創建數據庫會話工廠對象。
Reader reader = Resources.getResourceAsReader("resource.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 通過會話工廠獲取SqlSession對象,一個SqlSession對象代表和數據庫的一次會話
SqlSession session = sqlSessionFactory.openSession();
// 推薦使用namespace+id,防止多個配置文件中的id重復。
Employee emp = session.selectOne("hello.selOne", "1");
System.out.println(emp);
// 每次使用完需要關閉sqlsession對象。(必須)
session.close();
// 注意:SqlSession不是線程安全的,因此不能被共享。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 新增員工操作
*/
@Test
public void test4(){
SqlSession sqlSession=getSqlSession();
EmployeeDao employeeDao=sqlSession.getMapper(EmployeeDao.class);
Employee emp=new Employee("張三",'1',"常山");
int total=employeeDao.insertEmp(emp);
sqlSession.commit();//已經默認開啟了事務,需要手動的提交
System.out.println(total);
sqlSession.close();
}
~~~
- spring
- 1.spring第一天
- 1.1 安裝spring插件(spring tool suite)
- 1.2 spring概述
- 1.3 控制反轉&依賴注入
- 1.4 springIOC容器
- 1.5 依賴注入的四種方式
- 1.6 配置bean的細節
- 1.7 bean之間的關系
- 1.8 bean作用域
- 1.9 補充:創建對象的幾種方法
- 1源代碼位置
- 2.spring第二天
- 2.1 使用外部屬性文件
- 2.2 spEL
- 2.3 bean的生命周期
- 2.4 通過工廠方式配置bean
- 2.5 基于注解的方式配置bean
- 2.6 組件裝配
- 2.7 靜態代理
- 2.8 動態代理
- 2.9 Cglib代理
- 2源代碼位置
- 3. spring第三天
- 3.1 springAOP
- 3.1.1 AOP簡介
- 3.1.2 為什么使用AOP
- 3.1.3 AOP關鍵術語
- 3.1.4 AOP圖解
- 3.1.5 springAOP實現步驟
- 3.1.6 SpringAOP實現原理:
- 3.1.7 AOP的好處
- 3.1.8 AOP在實際項目中的主要應用
- 3代碼地址
- 3.1.9 純注解版配置aop的方式
- 3.2 maven環境搭建
- 附IDEA激活碼
- 4. spring第四天
- 4.1 c3p0事務
- 4.2 命令窗口事務
- 4.3 c3p0連接池設置
- 4.4 事務中的一些基本概念
- 4.5 事務的傳播行為
- 4.6 自定義異常
- 4.7 spring整合Junit單元測試
- 4.8 JdbcTemplate(附源代碼)
- 事務源代碼
- 4.9 純注解tx
- 4.10 基于xml配置事務
- 0. jsp頁面修改編碼方式
- 0.1 eclipse配置tomcat
- 0.單例模式-飽漢模式
- 0.單例模式-饑漢模式
- springMVC
- 1. springmvc第一天
- 1.1 springMVC概述
- 1.2 springmvc框架搭建及第一個應用程序
- 1.3 @RequestMapping
- 1.4 RequestMapping修飾類
- 1.5 RequestMapping精準化映射
- 1.6 Ant風格URL
- 1.7 帶有占位符的url映射
- 1.8 REST風格
- 1.9 RequerstParam獲取請求正文
- 2. springmvc第二天
- 2.1 優化
- 2.2 POJO綁定請求參數
- 2.3 RequestHeader獲取請求報頭信息
- 2.4 CookieValue獲取Cookie信息
- 2.5 獲取原生ServletAPI
- 2.6 ModelAndView處理模型數據
- 2.7 Map、Model、ModelMap處理模型數據
- 2.8 @SessionAttributes注解
- 2.9 @ModelAttribute無返回值方法及方法入參
- 2.10 @ModelAttribute修飾有返回值類型的方法
- 代碼地址
- 3. springmvc補充
- 3-1 springmvc工作原理
- 3-2 springmvc form表單提交中文亂碼
- 3-3 數據的格式化
- 3-4 自定義類型轉換器
- 3-5 其他知識點
- 3-6 crud代碼
- 3-7 @DateTimeFormat日期格式化
- 3-8 數據驗證的概念及JSR303驗證
- 3-9 Hibernate-Validator驗證框架
- 3-10 Controller捕獲錯誤消息
- 3-11 errors標簽在頁面中獲取錯誤消息
- 3-12 錯誤消息的定制及國際化
- 3-13 自定義攔截器
- 3-14 Java代碼中獲取國際化信息
- 3-15 超級鏈接設置國際化
- 3-16 AJAX支持之@RequestBody
- mybatis
- 1. mybatis第一天
- 1. 為什么使用mybatis
- 2. 下載地址
- 3. hello
- 4. mybatis三種開發模式
- 5. 全局配屬屬性內容
- 6. DTD設置
- 7. Mapper中的CRUD
- 8. 8.mybatis使用主鍵自增
- 9. #{}中的參數處理
- 10. #{}與${}區別
- 11. 集合數據的查詢
- 12 動態sql
- 12.1 if
- 12.2 choose, when, otherwise
- 12.3 trim, where, set
- 12.4 foreach
- 代碼位置
- 2. mybatis第二天
- 1.封裝map類型的數據
- 2. resultMap自定義封裝規則
- 0代碼位置
- 3. mybatis緩存機制
- ssm整合
- 1.maven
- 2.ssm基礎環境搭建
- 2-1 引入項目依賴的jar包
- 2-2 引入bootstrap,jquery
- 2-3 創建項目包結構
- 2-4 編寫web.xml配置文件
- 2-5 編寫sping,springmvc,mybatis配置文件
- 2-6 逆向工程mbg.xml
- shiro安全框架
- 1.shiro簡介
- 易購Buy商城
- 第一天
- 1.課程計劃
- 2.電商行業背景
- 3.易購Buy介紹
- 4.易購Buy架構
- 5.工程搭建
- 6.工程啟動和測試
- 7.ssm框架整合
- 8.整合測試
- 9.svn
- 9.1 svn服務端
- 9.2 svn客戶端
- 第二天
- 1.SOA架構分析
- 2.dubbo使用方法
- 3.注冊中心
- 4.工程改造
- 5.easyUI
- maven
- 1.maven介紹
- 2.idea配置maven和服務器
- 3.創建web工程
- 4.分模塊構建工程
- 5. 代碼位置
- 6. nexus
- Luence搜索
- 1.了解搜索技術
- 2.Lucene的基本使用
- solr
- SolrCloud