MyBatis using Crud

Source: Internet
Author: User

MyEclipse does not provide automatic generation, where mybatis packages and development documentation are provided http://download.csdn.net/detail/u010026901/7489319

Set up your own configuration file,

<?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>
<environments default=" Development ">
< Environment id= "Development" >
<transactionmanager type= "JDBC"   /> <datasource type=
"Pooled" ">
<property name=" Driver "value=" Oracle.jdbc.driver.OracleDriver "/> <property name=
" URL "value=" Jdbc:oracle:thin:@192.168.2.55:orcl "   />
<property name=" username "value=" Ysk "   >
<property name= "password" value= "123"   />
</dataSource>
</environment>
</environments>
<mappers>
<!--The daoimpl,mybatis that fills out DAO interface mappings is not the mapping Pojo (VO) class, but the DAO class-- >
    
<mapper resource= "Com/kane/dao/newsdaoimpl.xml"   />
</mappers>
</ Configuration>

Configure the linked Sqlsessionfactory class yourself, equivalent to a hibernate sessionfactory corresponding to a database
 

Package COM.KANE.DBC;
    
    
Import Java.io.InputStream;
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import Org.apache.ibatis.session.SqlSessionFactory;
    
    
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Mybatissqlsessionfactory {//configuration file location and name private static String config_file_location = "Mybatis-co
    
    
Nf.xml ";
Used to implement connection pooling, this class is similar to a map collection.
private static final threadlocal<sqlsession> ThreadLocal = new threadlocal<sqlsession> ();
MyBatis class private static inputstream is used to read configuration files;
Used to establish a connection, this class is the connection pool, using a single case design mode private static Sqlsessionfactory sqlsessionfactory;
    
    
Alternate configuration file location private static String configfile = Config_file_location; Static block, which executes the first static {try {///load configuration file into memory with the. = = Resources.getresourceasstream (configfile),//build connection pool and the inside connection SQLSESSIONFAC
Tory = new Sqlsessionfactorybuilder (). Build (IS); catch (Exception e) {System.err.println ("%%%% Error creating SessionfactOry%%%% ");
E.printstacktrace (); The private Mybatissqlsessionfactory () {}/** * Gets the database connection object * * @return session * @throws Hibernateexc
Eption/public static sqlsession getsession () {//Get the connection from Threadlocal first.
    
    
sqlsession session = (sqlsession) threadlocal.get ();
Threadlocal.set (session);
return to session; /** * Connection Shutdown method * * @throws hibernateexception/public static void CloseSession () {sqlsession session = (SQL
session) Threadlocal.get ();
Empties the threadlocal to indicate that the current thread is no longer connected.
Threadlocal.set (NULL);
Connection back to Connection pool if (session!= null) {Session.close ();}} }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.