- create table pda2_user(
- id varchar2(50) primary key,
- name varchar2(100) not null,
- psw varchar2(100) not null
- );
- ######################################
- create sequence pda2_user_sequence
- minvalue 1
- maxvalue 999999999999999999999999999
- start with 1
- increment by 1
- cache 20;
- #####################################
- create trigger "id_auto_increment_triger"
- before insert on pda2_user
- for each row
- when (new.id is null)
- begin
- select pda2_user_sequence.nextval into :new.id from dual;
- end;
- ###################################################
- insert into pad2_user (name,psw) values('chenjun','123')
- ####说明######
- 我使用的是Oracle数据库,为了使用主键自增长的方式,特此建立一个使用sequence的触发器
- package com.deppon.pda.v2.model.basic.User;
- public class User {
- private String ID;
- private String name;
- private String psw;
- public User() {
- super();
- }
- public User(String name, String psw) {
- super();
- this.name = name;
- this.psw = psw;
- }
- public String getID() {
- return ID;
- }
- public void setID(String iD) {
- ID = iD;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPsw() {
- return psw;
- }
- public void setPsw(String psw) {
- this.psw = psw;
- }
- }
- package com.deppon.pda.v2.mapper.User;
- import com.deppon.pda.v2.model.basic.User.User;
- public interface UserMapper {
- public void insertUser(User user);
- public User getUser(String name);
- }
- ?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.deppon.pda.v2.mapper.User.UserMapper">
- <insert id="insertUser" parameterType="com.deppon.pda.v2.model.basic.User.User">
- insert into pda2_user(name,age) values(#{name},#{age})
- </insert>
- <select id="getUser" resultType="com.deppon.pda.v2.model.basic.User.User" parameterType="java.lang.String">
- select * from pda2_user where name=#{name}
- </select>
- <!-- 当使用该Mybatis与Spring整合的时候,该文件必须和相应的Mapper接口文件同名,并在同一路径下 -->
- </mapper>
- xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- ">
- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
- </property>
- <property name="url"
- value="jdbc:oracle:thin:@192.168.56.201:1521:system">
- </property>
- <property name="username" value="vincent"></property>
- <property name="password" value="1234"></property>
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="myDataSource"/>
- <!-- <property name="configLocation" value=""/> -->
- <!-- 该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的
- 元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置 。当然如果你不打算添加typeAlias
- 之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了-->
- <!--<property name="mapperLocations" value="src/UserMapper.xml"/>-->
- <!-- 该配置文件用来指定Mapper映射文件的位置 ,如果映射文件与相应的接口同名,且在同一路径下,那么可以不配置该选项-->
- </bean>
- <!--注册Mapper方式一
- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="mapperInterface" value="com.deppon.pda.v2.mapper.User.UserMapper"/>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
- -->
- <!-- 注册Mapper方式二:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.deppon.pda.v2.mapper"/>
- </bean>
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="myDataSource"/>
- package loginTest;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import com.deppon.pda.v2.framework.CheckUser;
- import com.deppon.pda.v2.mapper.User.UserMapper;
- import com.deppon.pda.v2.model.basic.User.User;
- public class UserMapperTest {
- @Test
- public void testMapper() {
- ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
- UserMapper userMapper = aContext.getBean(UserMapper.class);
- User user = userMapper.getUser("chenjun");
- System.out.println(user.getPsw());
- }
- }