日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

primefaces_轻量级Web应用程序框架:PrimeFaces(JSF)+ Guice + MyBatis(第2部分)

發布時間:2023/12/3 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 primefaces_轻量级Web应用程序框架:PrimeFaces(JSF)+ Guice + MyBatis(第2部分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

primefaces

在這一部分中,我將繼續演示JSF,Guice和MyBatis的集成。 在持久層中使用DBCP連接池和MYSQL數據庫。 看一下第1部分 。

在上一篇文章中 ,我們創建了一個ServletContextListener。 現在,我們只需要在contextInitialized方法中綁定BasicDataSourceProvider和JdbcTransactionFactory。

GuiceContextListener.java

package org.borislam;import java.util.Properties;import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.apache.log4j.xml.DOMConfigurator; import org.borislam.mapper.StaffMapper; import org.borislam.service.SimpleService; import org.borislam.service.impl.SimpleServiceImpl; import org.mybatis.guice.MyBatisModule; import org.mybatis.guice.datasource.dbcp.BasicDataSourceProvider; import org.mybatis.guice.datasource.helper.JdbcHelper;import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.name.Names;public class GuiceContextListener implements ServletContextListener {public void contextDestroyed(ServletContextEvent servletContextEvent) {ServletContext servletContext = servletContextEvent.getServletContext();servletContext.removeAttribute(Injector.class.getName());}public void contextInitialized(ServletContextEvent servletContextEvent) {Injector injector = Guice.createInjector(new MyBatisModule() {@Overrideprotected void initialize() { install(JdbcHelper.MySQL);environmentId('development');bindDataSourceProviderType(BasicDataSourceProvider.class);bindTransactionFactoryType(JdbcTransactionFactory.class);Names.bindProperties(binder(), createServerProperties());//add singleton service classbind(SimpleService.class).to(SimpleServiceImpl.class).in(Singleton.class); //add MyBatis Service classaddMapperClass(StaffMapper.class);}});ServletContext servletContext = servletContextEvent.getServletContext();servletContext.setAttribute(Injector.class.getName(), injector);//log4JDOMConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource('log4j.xml'));}protected static Properties createServerProperties() {Properties myBatisProperties = new Properties();myBatisProperties.setProperty('JDBC.host', 'localhost');myBatisProperties.setProperty('JDBC.port', '3306');myBatisProperties.setProperty('JDBC.schema', 'ttcoach');myBatisProperties.setProperty('JDBC.username', 'root');myBatisProperties.setProperty('JDBC.password', '');myBatisProperties.setProperty('JDBC.autoCommit', 'false');return myBatisProperties;}}

Staff.java

package org.borislam.model;public class Staff {private String code;private String name;private String sex;private String tel;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;} }

StaffMapper.java

package org.borislam.mapper;import java.util.List;import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.borislam.model.Staff;public interface StaffMapper {final String SELECT_ALL = 'SELECT * FROM FREELANCER';final String SELECT_BY_CODE = 'SELECT * FROM FREELANCER WHERE CODE = #{code}';/*** Returns the list of all Freelancer instances from the database.* @return the list of all Freelancer instances from the database.*/@Select(SELECT_ALL)@Results(value = {@Result(property='code', column='code'),@Result(property='name', column='name'),@Result(property='sex', column='sex'),@Result(property='tel', column='tel')})List<Staff> selectAll();/*** Returns a Freelancer instance from the database.* @param id primary key value used for lookup.* @return A Freelancer instance with a primary key value equals to pk. null if there is no matching row.*/@Select(SELECT_BY_CODE)@Results(value = {@Result(property='code', column='code'),@Result(property='name', column='name'),@Result(property='sex', column='sex'),@Result(property='tel', column='tel')})Staff selectByCode(String code); }

SimpleService.java

package org.borislam.service;public interface SimpleService {public void doSimpleThing(); }

SimpleServiceImpl.java

package org.borislam.service.impl;import java.util.List;import org.borislam.mapper.StaffMapper; import org.borislam.model.Staff; import org.borislam.service.SimpleService; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import com.google.inject.Inject;public class SimpleServiceImpl implements SimpleService {private StaffMapper staffMapper;Logger logger = LoggerFactory.getLogger(this.getClass());@Injectpublic void setStaffMapper(StaffMapper staffMapper) {this.staffMapper = staffMapper;}public void doSimpleThing() {List<Staff> staffList = staffMapper.selectAll();logger.debug('size 1: ' + staffList.size());Staff staff = staffMapper.selectByCode('c001');logger.debug('Code1 : ' + staff.getCode());logger.debug('Name 1: ' + staff.getName());;} }

TestBean.java

package org.borislam.view;import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.inject.Inject; import org.borislam.service.SimpleService; import org.borislam.service.TestService;@ManagedBean @SessionScoped public class TestBean extends BasePageBean {private SimpleService sService;@Injectpublic void setsService(SimpleService sService) {this.sService = sService;}public String doTest(){System.out.println('test 1 inside backing bean...');sService.doSimpleThing();return '';}public String doTest2(){System.out.println('test 2 inside backing bean...');sService.doSimpleThing();return '';} }

index.xhtml

<html xmlns='http://www.w3c.org/1999/xhtml' xmlns:h='http://java.sun.com/jsf/html' xmlns:p='http://primefaces.org/ui'> <h:head> <style> .ui-widget, .ui-widget .ui-widget { font-size: 80% !important; } </style> </h:head> <h:body> <h:form><h:outputText value='#{msg['website.title']}' /><p:calendar id='popupButtonCal' showOn='button' /><p:commandButton value='TEST2' action='#{testBean.doTest}'/><p:editor/><br/> </h:form> </h:body> </html>


大功告成! 現在,您可以準備基于此框架編寫JSF應用程序。
參考: 輕量級Web應用程序框架: Programming Peaceally博客中的JCG合作伙伴 Boris Lam的PrimeFaces(JSF)+ Guice + MyBatis(PART 2) 。

翻譯自: https://www.javacodegeeks.com/2013/01/lightweight-web-application-framework-primefaces-jsf-guice-mybatis-part-2.html

primefaces

總結

以上是生活随笔為你收集整理的primefaces_轻量级Web应用程序框架:PrimeFaces(JSF)+ Guice + MyBatis(第2部分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。