mybatis 中xml相关属性的含义
<?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"> //mappe可以在cfg.xml做映射也可以在写sqlxml中直接做映射,namespace当前写//sql的xml <mapper namespace="cn.tssit.meteorserver.mapper.auto.MeteorDataAutoMapper"> //resutMap吧对应表映射,id 他的唯一标识,type是你封装的pojo, // column 数据库中字段属性,property是你封装字段的属性,jdbcType数据库字段类//型 <resultMap id="baseResultMap" type="cn.tssit.meteorserver.domain.MeteorData"> <id column="ID" property="id" jdbcType="DECIMAL" /> <result column="SURVEYDATE" property="surveydate" jdbcType="TIMESTAMP" /> <result column="USAF" property="usaf" jdbcType="NVARCHAR" /> <result column="TEMPERATURE" property="temperature" jdbcType="DECIMAL" /> <result column="PRESSURE" property="pressure" jdbcType="DECIMAL" /> <result column="WATER_VAPOR_PRESSURE" property="waterVaporPressure" jdbcType="DECIMAL" /> <result column="SEA_SURFACE_TEMPERATURE" property="seaSurfaceTemperature" jdbcType="DECIMAL" /> <result column="RELATIVE_HUMIDITY" property="relativeHumidity" jdbcType="DECIMAL" /> <result column="WIND_DIR" property="windDir" jdbcType="DECIMAL" /> <result column="WIND_SPEED" property="windSpeed" jdbcType="DECIMAL" /> <result column="PRECIPITATION" property="precipitation" jdbcType="DECIMAL" /> <result column="PRECIPITATION_CODE" property="precipitationCode" jdbcType="DECIMAL" /> <result column="CEILING_HEIGHT" property="ceilingHeight" jdbcType="DECIMAL" /> <result column="OPAQUE_SKY_COVER" property="opaqueSkyCover" jdbcType="DECIMAL" /> <result column="TRANSPARENCY_SKY_COVER" property="transparencySkyCover" jdbcType="DECIMAL" /> <result column="HEIGHT_ABOVE_SEA_LEVEL" property="heightAboveSeaLevel" jdbcType="DECIMAL" /> <result column="OVERWATER_MIXING_HEIGHT" property="overwaterMixingHeight" jdbcType="DECIMAL" /> <result column="TEMPERATURE_LAPSE_RATE_BELOW" property="temperatureLapseRateBelow" jdbcType="DECIMAL" /> <result column="TEMPERATURE_LAPSE_RATE_ABOVE" property="temperatureLapseRateAbove" jdbcType="DECIMAL" /> <result column="TYPE" property="type" jdbcType="DECIMAL" /> <result column="DOMINANT_WAVE_PERIOD" property="dominantWavePeriod" jdbcType="DECIMAL" /> <result column="DOMINANT_WAVE_HEIGHT" property="dominantWaveHeight" jdbcType="DECIMAL" /> </resultMap> <sql id="selectSql"> select <foreach collection="selector.fields" item="item" index="index" separator=",">${item}</foreach> from ${selector.table} <if test="selector.joinTables != null"> <foreach collection="selector.joinTables" item="item" index="index" separator=" ">${item.join} join ${item} <if test="item.ons != null">on <foreach collection="item.ons" item="on" index="i" separator=" and ">${on.joinField}=${on.relateField}</foreach></if> </foreach> </if> <if test="selector.wheres != null"> WHERE <foreach collection="selector.wheres" item="item" index="index" separator=" "> <if test="item.prefix != null">${item.prefix}</if> <if test="item.value != null">#{item.value}</if> <if test="item.suffix != null">${item.suffix}</if> </foreach> </if> <if test="selector.groupFields != null"> group by <foreach collection="selector.groupFields" item="item" index="index" separator=",">${item}</foreach> </if> <if test="selector.orderFields != null"> order by <foreach collection="selector.orderFields" item="item" index="index" separator=",">${item}</foreach> </if> </sql> <sql id="whereSql"> <if test="wheres != null"> WHERE <foreach collection="wheres" item="item" index="index" separator=" "> <if test="item.prefix != null">${item.prefix}</if> <if test="item.value != null">#{item.value}</if> <if test="item.suffix != null">${item.suffix}</if> </foreach> </if> </sql> //id对应dao中使用唯一标识,parameterType输入参数的类型如果是传多个使用map//那么确保前段传过来的是key,value,requestType返回类型(一般封装后都是类类 //型),resultMap引用的表, // <include refid="selectSql"/>重复使用一条sql ,refid指向你要用sql id //连表查询时可以使用<requestMap extendx="对应表requestMap 的id"> <select id="select" parameterType="com.hjp.sqlbuild.Selector" resultMap="baseResultMap">//要用到表属性id <include refid="selectSql"/> </select> <select id="selectPage" parameterType="com.hjp.sqlbuild.Selector" resultMap="baseResultMap"> <include refid="selectSql"/> </select> <select id="selectForListMap" resultType="map" parameterType="com.hjp.sqlbuild.Selector"> <include refid="selectSql"/> </select> <select id="selectPageForListMap" resultType="map" parameterType="com.hjp.sqlbuild.Selector"> <include refid="selectSql"/> </select> <insert id="insert" parameterType="cn.tssit.meteorserver.domain.MeteorData" useGeneratedKeys="true" keyColumn="ID" keyProperty="id"> insert into TSSI_METEOR_DATA(ID,SURVEYDATE,USAF,TEMPERATURE,PRESSURE,WATER_VAPOR_PRESSURE,SEA_SURFACE_TEMPERATURE,RELATIVE_HUMIDITY,WIND_DIR,WIND_SPEED,PRECIPITATION,PRECIPITATION_CODE,CEILING_HEIGHT,OPAQUE_SKY_COVER,TRANSPARENCY_SKY_COVER,HEIGHT_ABOVE_SEA_LEVEL,OVERWATER_MIXING_HEIGHT,TEMPERATURE_LAPSE_RATE_BELOW,TEMPERATURE_LAPSE_RATE_ABOVE,TYPE,DOMINANT_WAVE_PERIOD,DOMINANT_WAVE_HEIGHT) values(#{id,jdbcType=DECIMAL},#{surveydate,jdbcType=TIMESTAMP},#{usaf,jdbcType=NVARCHAR},#{temperature,jdbcType=DECIMAL},#{pressure,jdbcType=DECIMAL},#{waterVaporPressure,jdbcType=DECIMAL},#{seaSurfaceTemperature,jdbcType=DECIMAL},#{relativeHumidity,jdbcType=DECIMAL},#{windDir,jdbcType=DECIMAL},#{windSpeed,jdbcType=DECIMAL},#{precipitation,jdbcType=DECIMAL},#{precipitationCode,jdbcType=DECIMAL},#{ceilingHeight,jdbcType=DECIMAL},#{opaqueSkyCover,jdbcType=DECIMAL},#{transparencySkyCover,jdbcType=DECIMAL},#{heightAboveSeaLevel,jdbcType=DECIMAL},#{overwaterMixingHeight,jdbcType=DECIMAL},#{temperatureLapseRateBelow,jdbcType=DECIMAL},#{temperatureLapseRateAbove,jdbcType=DECIMAL},#{type,jdbcType=DECIMAL},#{dominantWavePeriod,jdbcType=DECIMAL},#{dominantWaveHeight,jdbcType=DECIMAL}) </insert> <insert id="insertSelective" parameterType="cn.tssit.meteorserver.domain.MeteorData" useGeneratedKeys="true" keyColumn="ID" keyProperty="id"> insert into TSSI_METEOR_DATA <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> ID, </if> <if test="surveydate != null"> SURVEYDATE, </if> <if test="usaf != null"> USAF, </if> <if test="temperature != null"> TEMPERATURE, </if> <if test="pressure != null"> PRESSURE, </if> <if test="waterVaporPressure != null"> WATER_VAPOR_PRESSURE, </if> <if test="seaSurfaceTemperature != null"> SEA_SURFACE_TEMPERATURE, </if> <if test="relativeHumidity != null"> RELATIVE_HUMIDITY, </if> <if test="windDir != null"> WIND_DIR, </if> <if test="windSpeed != null"> WIND_SPEED, </if> <if test="precipitation != null"> PRECIPITATION, </if> <if test="precipitationCode != null"> PRECIPITATION_CODE, </if> <if test="ceilingHeight != null"> CEILING_HEIGHT, </if> <if test="opaqueSkyCover != null"> OPAQUE_SKY_COVER, </if> <if test="transparencySkyCover != null"> TRANSPARENCY_SKY_COVER, </if> <if test="heightAboveSeaLevel != null"> HEIGHT_ABOVE_SEA_LEVEL, </if> <if test="overwaterMixingHeight != null"> OVERWATER_MIXING_HEIGHT, </if> <if test="temperatureLapseRateBelow != null"> TEMPERATURE_LAPSE_RATE_BELOW, </if> <if test="temperatureLapseRateAbove != null"> TEMPERATURE_LAPSE_RATE_ABOVE, </if> <if test="type != null"> TYPE, </if> <if test="dominantWavePeriod != null"> DOMINANT_WAVE_PERIOD, </if> <if test="dominantWaveHeight != null"> DOMINANT_WAVE_HEIGHT, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=DECIMAL}, </if> <if test="surveydate != null"> #{surveydate,jdbcType=TIMESTAMP}, </if> <if test="usaf != null"> #{usaf,jdbcType=NVARCHAR}, </if> <if test="temperature != null"> #{temperature,jdbcType=DECIMAL}, </if> <if test="pressure != null"> #{pressure,jdbcType=DECIMAL}, </if> <if test="waterVaporPressure != null"> #{waterVaporPressure,jdbcType=DECIMAL}, </if> <if test="seaSurfaceTemperature != null"> #{seaSurfaceTemperature,jdbcType=DECIMAL}, </if> <if test="relativeHumidity != null"> #{relativeHumidity,jdbcType=DECIMAL}, </if> <if test="windDir != null"> #{windDir,jdbcType=DECIMAL}, </if> <if test="windSpeed != null"> #{windSpeed,jdbcType=DECIMAL}, </if> <if test="precipitation != null"> #{precipitation,jdbcType=DECIMAL}, </if> <if test="precipitationCode != null"> #{precipitationCode,jdbcType=DECIMAL}, </if> <if test="ceilingHeight != null"> #{ceilingHeight,jdbcType=DECIMAL}, </if> <if test="opaqueSkyCover != null"> #{opaqueSkyCover,jdbcType=DECIMAL}, </if> <if test="transparencySkyCover != null"> #{transparencySkyCover,jdbcType=DECIMAL}, </if> <if test="heightAboveSeaLevel != null"> #{heightAboveSeaLevel,jdbcType=DECIMAL}, </if> <if test="overwaterMixingHeight != null"> #{overwaterMixingHeight,jdbcType=DECIMAL}, </if> <if test="temperatureLapseRateBelow != null"> #{temperatureLapseRateBelow,jdbcType=DECIMAL}, </if> <if test="temperatureLapseRateAbove != null"> #{temperatureLapseRateAbove,jdbcType=DECIMAL}, </if> <if test="type != null"> #{type,jdbcType=DECIMAL}, </if> <if test="dominantWavePeriod != null"> #{dominantWavePeriod,jdbcType=DECIMAL}, </if> <if test="dominantWaveHeight != null"> #{dominantWaveHeight,jdbcType=DECIMAL}, </if> </trim> </insert> <update id="update" parameterType="map"> update TSSI_METEOR_DATA set SURVEYDATE = #{record.surveydate,jdbcType=TIMESTAMP} ,USAF = #{record.usaf,jdbcType=NVARCHAR} ,TEMPERATURE = #{record.temperature,jdbcType=DECIMAL} ,PRESSURE = #{record.pressure,jdbcType=DECIMAL} ,WATER_VAPOR_PRESSURE = #{record.waterVaporPressure,jdbcType=DECIMAL} ,SEA_SURFACE_TEMPERATURE = #{record.seaSurfaceTemperature,jdbcType=DECIMAL} ,RELATIVE_HUMIDITY = #{record.relativeHumidity,jdbcType=DECIMAL} ,WIND_DIR = #{record.windDir,jdbcType=DECIMAL} ,WIND_SPEED = #{record.windSpeed,jdbcType=DECIMAL} ,PRECIPITATION = #{record.precipitation,jdbcType=DECIMAL} ,PRECIPITATION_CODE = #{record.precipitationCode,jdbcType=DECIMAL} ,CEILING_HEIGHT = #{record.ceilingHeight,jdbcType=DECIMAL} ,OPAQUE_SKY_COVER = #{record.opaqueSkyCover,jdbcType=DECIMAL} ,TRANSPARENCY_SKY_COVER = #{record.transparencySkyCover,jdbcType=DECIMAL} ,HEIGHT_ABOVE_SEA_LEVEL = #{record.heightAboveSeaLevel,jdbcType=DECIMAL} ,OVERWATER_MIXING_HEIGHT = #{record.overwaterMixingHeight,jdbcType=DECIMAL} ,TEMPERATURE_LAPSE_RATE_BELOW = #{record.temperatureLapseRateBelow,jdbcType=DECIMAL} ,TEMPERATURE_LAPSE_RATE_ABOVE = #{record.temperatureLapseRateAbove,jdbcType=DECIMAL} ,TYPE = #{record.type,jdbcType=DECIMAL} ,DOMINANT_WAVE_PERIOD = #{record.dominantWavePeriod,jdbcType=DECIMAL} ,DOMINANT_WAVE_HEIGHT = #{record.dominantWaveHeight,jdbcType=DECIMAL} <include refid="whereSql"/> </update> <update id="updateSelective" parameterType="map"> update TSSI_METEOR_DATA <set> <if test="record.surveydate != null"> SURVEYDATE = #{record.surveydate,jdbcType=TIMESTAMP}, </if> <if test="record.usaf != null"> USAF = #{record.usaf,jdbcType=NVARCHAR}, </if> <if test="record.temperature != null"> TEMPERATURE = #{record.temperature,jdbcType=DECIMAL}, </if> <if test="record.pressure != null"> PRESSURE = #{record.pressure,jdbcType=DECIMAL}, </if> <if test="record.waterVaporPressure != null"> WATER_VAPOR_PRESSURE = #{record.waterVaporPressure,jdbcType=DECIMAL}, </if> <if test="record.seaSurfaceTemperature != null"> SEA_SURFACE_TEMPERATURE = #{record.seaSurfaceTemperature,jdbcType=DECIMAL}, </if> <if test="record.relativeHumidity != null"> RELATIVE_HUMIDITY = #{record.relativeHumidity,jdbcType=DECIMAL}, </if> <if test="record.windDir != null"> WIND_DIR = #{record.windDir,jdbcType=DECIMAL}, </if> <if test="record.windSpeed != null"> WIND_SPEED = #{record.windSpeed,jdbcType=DECIMAL}, </if> <if test="record.precipitation != null"> PRECIPITATION = #{record.precipitation,jdbcType=DECIMAL}, </if> <if test="record.precipitationCode != null"> PRECIPITATION_CODE = #{record.precipitationCode,jdbcType=DECIMAL}, </if> <if test="record.ceilingHeight != null"> CEILING_HEIGHT = #{record.ceilingHeight,jdbcType=DECIMAL}, </if> <if test="record.opaqueSkyCover != null"> OPAQUE_SKY_COVER = #{record.opaqueSkyCover,jdbcType=DECIMAL}, </if> <if test="record.transparencySkyCover != null"> TRANSPARENCY_SKY_COVER = #{record.transparencySkyCover,jdbcType=DECIMAL}, </if> <if test="record.heightAboveSeaLevel != null"> HEIGHT_ABOVE_SEA_LEVEL = #{record.heightAboveSeaLevel,jdbcType=DECIMAL}, </if> <if test="record.overwaterMixingHeight != null"> OVERWATER_MIXING_HEIGHT = #{record.overwaterMixingHeight,jdbcType=DECIMAL}, </if> <if test="record.temperatureLapseRateBelow != null"> TEMPERATURE_LAPSE_RATE_BELOW = #{record.temperatureLapseRateBelow,jdbcType=DECIMAL}, </if> <if test="record.temperatureLapseRateAbove != null"> TEMPERATURE_LAPSE_RATE_ABOVE = #{record.temperatureLapseRateAbove,jdbcType=DECIMAL}, </if> <if test="record.type != null"> TYPE = #{record.type,jdbcType=DECIMAL}, </if> <if test="record.dominantWavePeriod != null"> DOMINANT_WAVE_PERIOD = #{record.dominantWavePeriod,jdbcType=DECIMAL}, </if> <if test="record.dominantWaveHeight != null"> DOMINANT_WAVE_HEIGHT = #{record.dominantWaveHeight,jdbcType=DECIMAL}, </if> </set> <include refid="whereSql"/> </update> <delete id="delete" parameterType="map"> delete from TSSI_METEOR_DATA <include refid="whereSql"/> </delete> </mapper>
具体实例
1、数据库
在MySQL中,test数据库下,简历user表,字段:id,name,password,建表语句略。
2、需导入的包
只有两个: mybatis-3.0.3.jar mysql-connector-java-5.1.16-bin.jar(JDBC包)
3、目录结构
采用最简单的结构,com.mybatis包下有且仅有四个文件:configuration.xml、user.xml、User.java、Test.java 。
4、mybatis配置文件configuration.xml
<?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> <typeAliases> <typeAlias alias="User" type="com.mybatis.User"></typeAlias> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="user.xml"/> </mappers> </configuration>
5、User表sql文件User.xml
<?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="User"> <select id="selectUser" parameterType="int" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> <select id="selectUsers" resultType="User"> SELECT * FROM user </select> </mapper>
6、表结构文件User.java
package com.mybatis; public class User { private int id; private String name; private String password; public User(){} public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + this.id + ", name=" + this.name + ", password=" + this.password + "]"; } }
7、测试用例Test.java
package com.mybatis; import java.io.IOException; import java.io.Reader; import java.util.List; 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 Test { public static void main(String[] args) throws IOException { String resource = "configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); try{ User user = session.selectOne("selectUser", "1"); System.out.println(user.getName()); System.out.println(user); System.out.println("--------------分隔线---------------"); List<User> users = session.selectList("selectUsers"); for(int i=0; i<users.size(); i++) { System.out.println(users.get(i).getName()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } }
package
com.mybatis;
public
class
User {
private
int
id;
private
String name;
private
String password;
public
User(){}
public
User(
int
id, String name) {
this
.id = id;
this
.name = name;
}
public
int
getId() {
return
this
.id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
this
.name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPassword() {
return
this
.password;
}
public
void
setPassword(String password) {
this
.password = password;
}
@Override
public
String toString() {
return
"User [id="
+
this
.id +
", name="
+
this
.name +
", password="
+
this
.password +
"]"
;
}
}
7、测试用例Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.mybatis; import java.io.IOException; import java.io.Reader; import java.util.List; 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 Test { public static void main(String[] args) throws IOException { String resource = "configuration.xml" ; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); try { User user = session.selectOne( "selectUser" , "1" ); System.out.println(user.getName()); System.out.println(user); System.out.println( "--------------分隔线---------------" ); List<User> users = session.selectList( "selectUsers" ); for ( int i= 0 ; i<users.size(); i++) { System.out.println(users.get(i).getName()); } } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } } } |
以上所述是小编给大家介绍的Java持久层框架MyBatis简单实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区
您可能感兴趣的文章:
- Java实现mybatis批量插入数据到Oracle
- Java的MyBatis框架中Mapper映射配置的使用及原理解析
- Java的MyBatis+Spring框架中使用数据访问对象DAO模式的方法
- Java的MyBatis框架中关键的XML字段映射的配置参数详解
- 实例讲解Java的MyBatis框架对MySQL中数据的关联查询
- 详解Java的MyBatis框架中的事务处理
- Java环境中MyBatis与Spring或Spring MVC框架的集成方法
- 详解Java的MyBatis框架和Spring框架的整合运用
- Java的MyBatis框架项目搭建与hellow world示例
- java MyBatis拦截器Inteceptor详细介绍
最新评论