freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

傳智播客springmvcmybatis由淺入深全套視頻教程v教案(編輯修改稿)

2025-05-13 22:49 本頁面
 

【文章內(nèi)容簡(jiǎn)介】 的靜態(tài)字段或?qū)嵗侄沃小?打開一個(gè) SqlSession;使用完畢就要關(guān)閉它。通常把這個(gè)關(guān)閉操作放到 finally 塊中以確保每次都能執(zhí)行關(guān)閉。如下: SqlSession session = ()。 try { // do work } finally { ()。 } 原始Dao開發(fā)方式 原始Dao開發(fā)方法需要程序員編寫Dao接口和Dao實(shí)現(xiàn)類。 映射文件?xml version= encoding=UTF8 ?!DOCTYPE mapperPUBLIC //mapper namespace=test! 根據(jù)id獲取用戶信息 select id=findUserById parameterType=int resultType= select * from user where id = {id} /select! 添加用戶 insert id=insertUser parameterType= selectKey keyProperty=id order=AFTER resultType= select LAST_INSERT_ID() /selectKey insert into user(username,birthday,sex,address) values({username},{birthday},{sex},{address}) /insert/mapper Dao接口Public interface UserDao { public User getUserById(int id) throws Exception。 public void insertUser(User user) throws Exception。}Public class UserDaoImpl implements UserDao { //注入SqlSessionFactory public UserDaoImpl(SqlSessionFactory sqlSessionFactory){ (sqlSessionFactory)。 } private SqlSessionFactory sqlSessionFactory。 @Override public User getUserById(int id) throws Exception { SqlSession session = ()。 User user = null。 try { //通過sqlsession調(diào)用selectOne方法獲取一條結(jié)果集 //參數(shù)1:指定定義的statement的id,參數(shù)2:指定向statement中傳遞的參數(shù) user = (, 1)。 (user)。 } finally{ ()。 } return user。 } @Override Public void insertUser(User user) throws Exception { SqlSession sqlSession = ()。 try { (insertUser, user)。 ()。 } finally{ ()。 } }} 問題原始Dao開發(fā)中存在以下問題:u Dao方法體存在重復(fù)代碼:通過SqlSessionFactory創(chuàng)建SqlSession,調(diào)用SqlSession的數(shù)據(jù)庫操作方法u 調(diào)用sqlSession的數(shù)據(jù)庫操作方法需要指定statement的id,這里存在硬編碼,不得于開發(fā)維護(hù)。 Mapper動(dòng)態(tài)代理方式 實(shí)現(xiàn)原理 Mapper接口開發(fā)方法只需要程序員編寫Mapper接口(相當(dāng)于Dao接口),由Mybatis框架根據(jù)接口定義創(chuàng)建接口的動(dòng)態(tài)代理對(duì)象,代理對(duì)象的方法體同上邊Dao接口實(shí)現(xiàn)類方法。Mapper接口開發(fā)需要遵循以下規(guī)范: 。 的parameterType的類型相同 (映射文件) (),需要修改namespace的值為 UserMapper接口路徑。 下mapper目錄 下。?xml version= encoding=UTF8 ?!DOCTYPE mapperPUBLIC //mapper namespace=! 根據(jù)id獲取用戶信息 select id=findUserById parameterType=int resultType= select * from user where id = {id} /select! 自定義條件查詢用戶列表 select id=findUserByUsername parameterType= resultType= select * from user where username like 39。%${value}%39。 /select! 添加用戶 insert id=insertUser parameterType= selectKey keyProperty=id order=AFTER resultType= select LAST_INSERT_ID() /selectKey insert into user(username,birthday,sex,address) values({username},{birthday},{sex},{address}) /insert/mapper (接口文件)/** * 用戶管理mapper */Public interface UserMapper { //根據(jù)用戶id查詢用戶信息 public User findUserById(int id) throws Exception。 //查詢用戶列表 public ListUser findUserByUsername(String username) throws Exception。 //添加用戶信息 public void insertUser(User user)throws Exception。 }接口定義有如下特點(diǎn): : ! 加載映射文件 mappers mapper resource=mapper// /mappers 測(cè)試Public class UserMapperTest extends TestCase { private SqlSessionFactory sqlSessionFactory。 protected void setUp() throws Exception { //mybatis配置文件 String resource = 。 InputStream inputStream = (resource)。 //使用SqlSessionFactoryBuilder創(chuàng)建sessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream)。 } Public void testFindUserById() throws Exception { //獲取session SqlSession session = ()。 //獲取mapper接口的代理對(duì)象 UserMapper userMapper = ()。 //調(diào)用代理對(duì)象方法 User user = (1)。 (user)。 //關(guān)閉session ()。 } @Test public void testFindUserByUsername() throws Exception { SqlSession sqlSession = ()。 UserMapper userMapper = ()。 ListUser list = (張)。 (())。 }Public void testInsertUser() throws Exception { //獲取session SqlSession session = ()。 //獲取mapper接口的代理對(duì)象 UserMapper userMapper = ()。 //要添加的數(shù)據(jù) User user = new User()。 (張三)。 (new Date())。 (1)。 (北京市)。 //通過mapper接口添加用戶 (user)。 //提交 ()。 //關(guān)閉session ()。 } } 總結(jié)u selectOne和selectList()()是根據(jù)mapper接口方法的返回值決定,如果返回list則調(diào)用selectList方法,如果返回單個(gè)對(duì)象則調(diào)用selectOne方法。u namespacemybatis官方推薦使用mapper代理方法開發(fā)mapper接口,程序員不用編寫mapper接口實(shí)現(xiàn)類,使用mapper代理方法時(shí),輸入?yún)?shù)可以使用pojo包裝對(duì)象或map對(duì)象,保證dao的通用性。3 配置內(nèi)容:properties(屬性)settings(全局配置參數(shù))typeAliases(類型別名)typeHandlers(類型處理器)objectFactory(對(duì)象工廠)plugins(插件)environments(環(huán)境集合屬性對(duì)象)environment(環(huán)境子屬性對(duì)象)transactionManager(事務(wù)管理)dataSource(數(shù)據(jù)源)mappers(映射器) properties(屬性):,==jdbc:mysql://localhost:3306/mybatis=root=mysql:properties resource=/ environments default=development environment id=development transactionManager type=JDBC/ dataSource type=POOLED property name=driver value=${}/ property name=url value=${}/ property name=username value=${}/ property name=password value=${}/ /dataSource /environment /environments注意: MyBatis 將按照下面的順序來加載屬性:u 在 properties 元素體內(nèi)定義的屬性首先被讀取。 u 然后會(huì)讀取properties 元素中resource或 url 加載的屬性,它會(huì)覆蓋已讀取的同名屬性。 u 最后讀取parameterType傳遞的屬性,它會(huì)覆蓋已讀取的同名屬性。因此,通過parameterType傳遞的屬性具有最高優(yōu)先級(jí),resource或 url 加載的屬性次之,最低優(yōu)先級(jí)的是 properties 元素體內(nèi)定義的屬性。 settings(配置)mybatis全局配置參數(shù),全局參數(shù)將會(huì)影響mybatis的運(yùn)行行為。詳細(xì)參見“學(xué)習(xí)資料/”文件 typeAliases(類型別名) mybatis支持別名:別名映射的類型_byte byte _long long _short short _int int _integer int _double double _float float _boolean boolean string String byte Byte long Long short Short int Integer integer Integer double Double float Float boolean Boolean date Date decimal BigDecimal bigdecimal BigDecimal 自定義別名::typeAliases ! 單個(gè)別名定義 typeAlias alias=user type=/ ! 批量別名定義,掃描整個(gè)包下的類,別名為類名(首字母大寫或小寫都可以) package name=/ package name=其它包//typeAliases typeHandlers(類型處理器)
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)教案相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號(hào)-1