Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Topic: Mybatis3.0 query, save, update, delete data.

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Recently, I intend to develop a technical framework, and apply this framework to develop a forum. At first, I intend to use sturts2+spring+hibernate+jquery as the basis, then encapsulate it, and finally form a technical framework. But through this website, I learned that mybatis is more flexible and practical than hibernate. So gradually come into contact with mybatis.

The following is a development example of mybatis3.0. The java environment jdk5.0,ide is eclipse3.7 and the database is oracle11g.

1. Data environment configuration

Xml code

Xml code

two。 Database table mapping (DM_MYDWT) XML file

Xml code

MYDWT_DM, MYDWT_MC, YXBZ, XYBZ

Select

From EASSEN.DM_MYDWT

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

Delete from EASSEN.DM_MYDWT

Where MYDWT_DM =

# {mydwtDm,jdbcType=CHAR}

Insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ

XYBZ)

Values

(# {mydwtDm,jdbcType=CHAR}, # {mydwtMc,jdbcType=VARCHAR}

# {yxbz,jdbcType=CHAR}

# {xybz,jdbcType=CHAR})

Insert into EASSEN.DM_MYDWT

MYDWT_DM

MYDWT_MC

YXBZ

XYBZ

# {mydwtDm,jdbcType=CHAR}

# {mydwtMc}

# {yxbz,jdbcType=CHAR}

# {xybz,jdbcType=CHAR}

Update EASSEN.DM_MYDWT

MYDWT_MC = # {mydwtMc,jdbcType=VARCHAR}

YXBZ = # {yxbz,jdbcType=CHAR}

XYBZ = # {xybz,jdbcType=CHAR}

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

Update EASSEN.DM_MYDWT

Set MYDWT_MC =

# {mydwtMc,jdbcType=VARCHAR}

YXBZ = # {yxbz,jdbcType=CHAR}

XYBZ =

# {xybz,jdbcType=CHAR}

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

Xml code

MYDWT_DM, MYDWT_MC, YXBZ, XYBZ

Select

From EASSEN.DM_MYDWT

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

Delete from EASSEN.DM_MYDWT

Where MYDWT_DM =

# {mydwtDm,jdbcType=CHAR}

Insert into EASSEN.DM_MYDWT (MYDWT_DM, MYDWT_MC, YXBZ

XYBZ)

Values

(# {mydwtDm,jdbcType=CHAR}, # {mydwtMc,jdbcType=VARCHAR}

# {yxbz,jdbcType=CHAR}

# {xybz,jdbcType=CHAR})

Insert into EASSEN.DM_MYDWT

MYDWT_DM

MYDWT_MC

YXBZ

XYBZ

# {mydwtDm,jdbcType=CHAR}

# {mydwtMc}

# {yxbz,jdbcType=CHAR}

# {xybz,jdbcType=CHAR}

Update EASSEN.DM_MYDWT

MYDWT_MC = # {mydwtMc,jdbcType=VARCHAR}

YXBZ = # {yxbz,jdbcType=CHAR}

XYBZ = # {xybz,jdbcType=CHAR}

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

Update EASSEN.DM_MYDWT

Set MYDWT_MC =

# {mydwtMc,jdbcType=VARCHAR}

YXBZ = # {yxbz,jdbcType=CHAR}

XYBZ =

# {xybz,jdbcType=CHAR}

Where MYDWT_DM = # {mydwtDm,jdbcType=CHAR}

3. Database table (DM_MYDWT) java

Java code

Package com.pojo

Import java.io.Serializable

Public class DmMydwt implements Serializable {

/ * *

*

, /

Private static final long serialVersionUID = 1078518054837885063L

/ * *

* EASSEN.DM_MYDWT.MYDWT_DM

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String mydwtDm

/ * *

* EASSEN.DM_MYDWT.MYDWT_MC

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String mydwtMc

/ * *

* EASSEN.DM_MYDWT.YXBZ

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String yxbz

/ * *

* EASSEN.DM_MYDWT.XYBZ

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String xybz

Public String getMydwtDm () {

Return mydwtDm

}

Public void setMydwtDm (String mydwtDm) {

This.mydwtDm = mydwtDm

}

Public String getMydwtMc () {

Return mydwtMc

}

Public void setMydwtMc (String mydwtMc) {

This.mydwtMc = mydwtMc

}

Public String getYxbz () {

Return yxbz

}

Public void setYxbz (String yxbz) {

This.yxbz = yxbz

}

Public String getXybz () {

Return xybz

}

Public void setXybz (String xybz) {

This.xybz = xybz

}

}

Java code

Package com.pojo

Import java.io.Serializable

Public class DmMydwt implements Serializable {

/ * *

*

, /

Private static final long serialVersionUID = 1078518054837885063L

/ * *

* EASSEN.DM_MYDWT.MYDWT_DM

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String mydwtDm

/ * *

* EASSEN.DM_MYDWT.MYDWT_MC

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String mydwtMc

/ * *

* EASSEN.DM_MYDWT.YXBZ

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String yxbz

/ * *

* EASSEN.DM_MYDWT.XYBZ

* @ ibatorgenerated 2012-02-05 20:14:30

, /

Private String xybz

Public String getMydwtDm () {

Return mydwtDm

}

Public void setMydwtDm (String mydwtDm) {

This.mydwtDm = mydwtDm

}

Public String getMydwtMc () {

Return mydwtMc

}

Public void setMydwtMc (String mydwtMc) {

This.mydwtMc = mydwtMc

}

Public String getYxbz () {

Return yxbz

}

Public void setYxbz (String yxbz) {

This.yxbz = yxbz

}

Public String getXybz () {

Return xybz

}

Public void setXybz (String xybz) {

This.xybz = xybz

}

}

4.DAO

Java code

Package com.dao

Import com.pojo.DmMydwt

Public interface DmMydwtMapper {

/ * *

* delete according to the primary key

* Parameter: primary key

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int deleteByPrimaryKey (String mydwtDm)

/ * *

* insert, empty attribute will also be inserted

* Parameter: pojo object

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int insert (DmMydwt record)

/ * *

* insert, empty attribute will not be inserted

* Parameter: pojo object

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int insertSelective (DmMydwt record)

/ * *

* query according to the primary key

* parameters: query condition, primary key value

* return: object

* @ ibatorgenerated 2012-02-05 20:14:58

, /

DmMydwt selectByPrimaryKey (String mydwtDm)

/ * *

* the null value condition will not be changed to null according to the primary key modification.

* Parameter: 1. The value to be modified

* return: the number of successful modifications

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int updateByPrimaryKeySelective (DmMydwt record)

/ * *

* according to the primary key modification, the null value condition will be changed to null

* Parameter: 1. The value to be modified

* return: the number of successful modifications

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int updateByPrimaryKey (DmMydwt record)

}

Java code

Package com.dao

Import com.pojo.DmMydwt

Public interface DmMydwtMapper {

/ * *

* delete according to the primary key

* Parameter: primary key

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int deleteByPrimaryKey (String mydwtDm)

/ * *

* insert, empty attribute will also be inserted

* Parameter: pojo object

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int insert (DmMydwt record)

/ * *

* insert, empty attribute will not be inserted

* Parameter: pojo object

* return: number of deletions

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int insertSelective (DmMydwt record)

/ * *

* query according to the primary key

* parameters: query condition, primary key value

* return: object

* @ ibatorgenerated 2012-02-05 20:14:58

, /

DmMydwt selectByPrimaryKey (String mydwtDm)

/ * *

* the null value condition will not be changed to null according to the primary key modification.

* Parameter: 1. The value to be modified

* return: the number of successful modifications

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int updateByPrimaryKeySelective (DmMydwt record)

/ * *

* according to the primary key modification, the null value condition will be changed to null

* Parameter: 1. The value to be modified

* return: the number of successful modifications

* @ ibatorgenerated 2012-02-05 20:14:58

, /

Int updateByPrimaryKey (DmMydwt record)

}

5.mybatis loading

Java code

Package com.db

Import java.io.IOException

Import java.io.Reader

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 ConnectionFactory {

Private static SqlSessionFactory factory

Private static SqlSession sqlSession = null

/ / read the MyBatis configuration file and create a SqlSessionFactory

Static {

Try {

Reader reader = Resources.getResourceAsReader ("sqlMapConfig.xml")

Factory = new SqlSessionFactoryBuilder () .build (reader, "eassen")

} catch (IOException e) {

E.printStackTrace ()

}

}

/ * *

* obtain SqlSession

* @ return

, /

Public static SqlSession getSession () {

If (sqlSession = = null) {

SqlSession = factory.openSession ()

}

Return sqlSession

}

/ * *

* get database table mapping object information from configuration file

* @ param mapper

* @ return

, /

Public static T getMapper (Class mapper) {

SqlSession session = getSession ()

Return (T) session.getMapper (mapper)

}

/ * *

* data submission

, /

Public static void commit () {

SqlSession.commit ()

}

/ * *

* data rollback

, /

Public static void rollback () {

SqlSession.rollback ()

}

/ * *

* turn off sqlsession

, /

Public static void close () {

If (sqlSession! = null) {

SqlSession.close ()

}

}

}

Java code

Package com.db

Import java.io.IOException

Import java.io.Reader

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 ConnectionFactory {

Private static SqlSessionFactory factory

Private static SqlSession sqlSession = null

/ / read the MyBatis configuration file and create a SqlSessionFactory

Static {

Try {

Reader reader = Resources.getResourceAsReader ("sqlMapConfig.xml")

Factory = new SqlSessionFactoryBuilder () .build (reader, "eassen")

} catch (IOException e) {

E.printStackTrace ()

}

}

/ * *

* obtain SqlSession

* @ return

, /

Public static SqlSession getSession () {

If (sqlSession = = null) {

SqlSession = factory.openSession ()

}

Return sqlSession

}

/ * *

* get database table mapping object information from configuration file

* @ param mapper

* @ return

, /

Public static T getMapper (Class mapper) {

SqlSession session = getSession ()

Return (T) session.getMapper (mapper)

}

/ * *

* data submission

, /

Public static void commit () {

SqlSession.commit ()

}

/ * *

* data rollback

, /

Public static void rollback () {

SqlSession.rollback ()

}

/ * *

* turn off sqlsession

, /

Public static void close () {

If (sqlSession! = null) {

SqlSession.close ()

}

}

}

6. Data query, insert, update, delete test

Java code

Package com.test

Import com.dao.DmMydwtMapper

Import com.db.ConnectionFactory

Import com.pojo.DmMydwt

Public class MydTest {

Public static void main (String [] args) {

Try {

/ / get the object

DmMydwtMapper mydwtMapper = ConnectionFactory

.getMapper (com.dao.DmMydwtMapper.class)

/ / query information with a MYDWT_DM of "1001"

DmMydwt mydwt = mydwtMapper.selectByPrimaryKey ("1001")

System.out.println ("mybatis query Test: =")

System.out.println ("MYDWT_DM:" + mydwt.getMydwtDm ())

System.out.println ("MYDWT_MC:" + mydwt.getMydwtMc ())

/ / insert data

System.out.println ("mybatis Save Test: =")

Mydwt = new DmMydwt ()

Mydwt.setMydwtDm ("aaaa")

Mydwt.setMydwtMc ("mybatis Save")

Mydwt.setYxbz ("Y")

Mydwt.setXybz ("Y")

MydwtMapper.insert (mydwt)

/ / Update data

System.out.println ("mybatis update test: =")

Mydwt.setMydwtMc (new String ("mybatis save" .getBytes (), "UTF-8"))

MydwtMapper.updateByPrimaryKey (mydwt)

/ / Delete data

System.out.println ("mybatis deletion Test: =")

MydwtMapper.deleteByPrimaryKey ("1001")

ConnectionFactory.commit ()

} catch (Exception e) {

ConnectionFactory.rollback ()

E.printStackTrace ()

} finally {

/ / close sqlsession

ConnectionFactory.close ()

}

}

}

Java code

Package com.test

Import com.dao.DmMydwtMapper

Import com.db.ConnectionFactory

Import com.pojo.DmMydwt

Public class MydTest {

Public static void main (String [] args) {

Try {

/ / get the object

DmMydwtMapper mydwtMapper = ConnectionFactory

.getMapper (com.dao.DmMydwtMapper.class)

/ / query information with a MYDWT_DM of "1001"

DmMydwt mydwt = mydwtMapper.selectByPrimaryKey ("1001")

System.out.println ("mybatis query Test: =")

System.out.println ("MYDWT_DM:" + mydwt.getMydwtDm ())

System.out.println ("MYDWT_MC:" + mydwt.getMydwtMc ())

/ / insert data

System.out.println ("mybatis Save Test: =")

Mydwt = new DmMydwt ()

Mydwt.setMydwtDm ("aaaa")

Mydwt.setMydwtMc ("mybatis Save")

Mydwt.setYxbz ("Y")

Mydwt.setXybz ("Y")

MydwtMapper.insert (mydwt)

/ / Update data

System.out.println ("mybatis update test: =")

Mydwt.setMydwtMc (new String ("mybatis save" .getBytes (), "UTF-8"))

MydwtMapper.updateByPrimaryKey (mydwt)

/ / Delete data

System.out.println ("mybatis deletion Test: =")

MydwtMapper.deleteByPrimaryKey ("1001")

ConnectionFactory.commit ()

} catch (Exception e) {

ConnectionFactory.rollback ()

E.printStackTrace ()

} finally {

/ / close sqlsession

ConnectionFactory.close ()

}

}

}

7. Console information output

Java code

2012-02-08 11:43:01 Logging initialized using 'org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl'adapter.

2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.

2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.

2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.

2012-02-08 11:43:01 PooledDataSource forcefully closed/removed all connections.

2012-02-08 11:43:02 Created connection 17671659.

2012-02-08 11:43:02 ooo Connection Opened

2012-02-08 11:43:02 = > Executing: select MYDWT_DM, MYDWT_MC, YXBZ, XYBZ from EASSEN.DM_MYDWT where MYDWT_DM =?

2012-02-08 11:43:02 = > Parameters: 1001 (String)

2012-02-08 11:43:02 Parameters: aaaa (String), mybatis save (String), Y (String), Y (String)

Mybatis update test: =

2012-02-08 11:43:02 = > Executing: update EASSEN.DM_MYDWT set MYDWT_MC =?, YXBZ =?, XYBZ =? Where MYDWT_DM =?

2012-02-08 11:43:02 = > Parameters: mybatis save (String), Y (String), Y (String), aaaa (String)

Mybatis delete test: =

2012-02-08 11:43:02 = > Executing: delete from EASSEN.DM_MYDWT where MYDWT_DM =?

2012-02-08 11:43:02 = > Parameters: 1001 (String)

2012-02-08 11:43:02 xxx Connection Closed

2012-02-08 11:43:02 Returned connection 17671659 to pool.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report