039Sprng與MyBatis整合(最簡單的)

2021-10-23 11:35:30 字數 2835 閱讀 2360

步驟

開發service元件

配置bean

栗子:資料表

create

table person

( person_id int

auto_increment

primary

key,

person_name varchar

(225),

person_age int

);

public

inte***ce

namespace

=>

"queryperson"

resultmap

="personmap"

>

select * from person where person_id between # and #

select

>

"addperson"

>

insert into person value (

>

test

="id != null"

>

#when

>

>

null

otherwise

>

choose

>

,#,#)

insert

>

"personmap"

type

="person"

>

column

="person_id"

property

="id"

/>

column

="person_name"

property

="name"

/>

column

="person_age"

property

="age"

/>

resultmap

>

>

public

inte***ce

personservice

public

class

personserviceimpl

implements

personservice

@override

public list

selectperson

(int startid,

int endid)

@override

public

intinsertperson

(person person)

}

<?xml version="1.0" encoding="utf-8"?>

xmlns

=""xmlns:context

=""xmlns:p

=""xmlns:c

=""xmlns:mybatis

=""xmlns:xsi

=""xsi:schemalocation

=".xsd"

>

"service"

class

="com.lyx.service.personserviceimpl"

>

name

= ref

=/>

bean

>

class

=>

name

= value

=/>

name

="sqlsessionfactory"

ref="sqlsessionfactory"

/>

bean

>

"sqlsessionfactory"

class

="org.mybatis.spring.sqlsessionfactorybean"

>

name

="configlocation"

value

="mybatis-config.xml"

/>

name

="datasource"

ref="c3p0"

/>

bean

>

<

context:property-placeholder

location

="classpath:db.properties"

/>

"c3p0"

class

="com.mchange.v2.c3p0.combopooleddatasource"

p:driverclass

="$"

p:jdbcurl

="$"

p:user

="$"

p:password

="$"

p:maxpoolsize

="$"

p:minpoolsize

="$"

/>

beans

>

從service元件開始配

sqlsessionfactory需要用org.mybatis.spring.sqlsessionfactorybean工廠類生成,

需要配置屬性:

datasource:資料來源

configlocation:mybatis的核心配置檔案(非必須)

配置資料來源

收藏)MyBatis與Spring整合

1 spring mybatis.xml 整合的配置,三種方式選擇一種即可。select from agent info where id 具體呼叫的 如下 1 使用sqlsessiontemplate方式 package com.research.mybatis.spring import org...

Spring與Mybatis整合過程

spring與mybatis整合過程用網上教學專案講解 1.整合前準備 2.實現spring對mybatis整合 配置datasource 配置sqlsessionfactorybean sqlsessiontemplate進行持久化操作 一.整合前準備 新增jar包 監理開發目錄結構,建立實體類 ...

spring 與 mybatis整合過程

第一步 整合dao層 第二步 整合service層 通過spring管理 service介面。使用配置方式將service介面配置在spring配置檔案中。實現事務控制。第三步 整合springmvc 由於springmvc是spring的模組,不需要整合。主要配置有 1 mybatis配置檔案sq...