Session acquisition of Java EE framework

Source: Internet
Author: User

1, MyBatis sqlsession get Mapperdao, there are two kinds of

1.1, the use of independent framework

Global configuration file (Sqlmapconfig.xml) +sqlsession Get method +mapperdao

Sqlsession Get Statement

String config= "Sqlmapconfig.xml"; Reader reader=resources.getresourceasreader (config); Sqlsessionfactorybuilder sfb=new Sqlsessionfactorybuilder (); Sqlsessionfactory Factory=sfb.build (reader); Sqlsession session=factory.opensession ();//Get map Dao//<mappers>:<mapper>: ' Resource ' Represents the attribute Resource-->sqlmapconfig.xml--><mappers>:<mapper> under the mapper element under the mappers element;: ' Resource '- After costmapperdao;//obtained session: Empmapdao Empdao=session.getmapper (Empmapdao.class);

  

Sqlmapconfig.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE configuration Public "-//ibatis.apache.org//dtd Config 3.0//en" "http://ibatis.apache.org/dtd/ Ibatis-3-config.dtd "><configuration><environments default=" Environment "><environment id=" Environment "><transactionmanager type=" JDBC "/><datasource type=" pooled "><property name=" Driver " Value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql://localhost:3306/test?useunicode= True&characterencoding=utf8 "/><property name=" username "value=" root "/><property name=" password " Value= "123456"/></datasource></environment><!--Oracle database--><!--<environment id= " Environment "><transactionmanager type=" JDBC "/><datasource type=" pooled "><property name=" Driver " Value= "Oracle.jdbc.OracleDriver"/><property name= "url" value= "jdbc:oracle:thin:@192.168.0.23:1521: tarena10g "/><property name=" username "value=" demo "/><propeRty name= "Password" value= "demo"/></datasource></environment>--></environments><!-- The XML file that loads the SQL statement--><mappers><mapper resource= "Com/jams/ibatis/entity/costsql.xml"/><mapper Resource= "Com/jams/ibatis/entity/usersql.xml"/><mapper resource= "Com/jams/ibatis/entity/empsql.xml"/>  </mappers></configuration>

 

1.2, integration with SPRINGMVC framework to obtain

Spring config file +mapperdao (no MyBatis global profile required)

Applicationcontext.xml

<!--sqlsessionfactory--><!--dbcp--><bean id= "dbcp" class= "Org.apache.commons.dbcp.BasicDataSource "><property name=" username "value=" root "/><property name=" password "value=" 123456 "/><property Name= "url" value= "Jdbc:mysql://localhost:3306/mynote?useunicode=true&characterencoding=utf8"/>< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/></bean><bean id= "SSF" class= " Org.mybatis.spring.SqlSessionFactoryBean "><!--parameters 1dbcp--><property name=" DataSource "ref=" dbcp "/> <!--parameter 2mapper file location--><property name= "Mapperlocations" value= "Classpath:com/jams/note/mapper/*.xml"/> </bean><!--mapperscanner, scan Mapperdao, instance DAO only needs @resource injection, you can use--><bean id= "Mapperscanner" in the first lowercase class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><!--Basic Package--><property name= "Basepackage" Value= "Com.jams.note.dao"/><!--sqlsessionfactory--><!--<property name= "Sqlsessionfactory" ref="SSF"/>--><!--restricted access--><!--<property name= "Annotationclass" value= "/>--></bean> 

Get an instance of Mapperdao

Bookdao=ac.getbean ("Notebookmapperdao", Notebookmapperdao.class); @resourceprivate Notebookmapperdao notebookmapperdao;method{    //notebookmapperdao ....}

  

2. Hibernate Framework gets session

2.1. The frame is used alone

String config= "Hibernate.cfg.xml"; Configuration conf=new configuration (); Conf.configure ();//Get Sessionfactorysf=conf.buildsessionfactory ();// Get Sessionsession session=sf.opensession ();

  

Session acquisition of Java EE framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.