<?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>
<!-- 这个是po类的设置 这样不必在sql中写po类的全限定名 -->
<typeAliases> <typeAlias alias="Photo" type="com.bjsxt.po.Photo"/> </typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/photo" />
<property name="username" value="root" />
<property name="password" value="xiaohang" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- sql 映射文件 这个是对应的配置文件 这个配置文件和dao里面的接口类联系 -->
<mapper resource="com/smallang/dao/PhotoDao.xml" />
</mappers>
</configuration>
public interface PhotoDao {
public void savePhoto(Photo photo);
public Photo getPhotoById(int id);
public Photo findPhotoByName(String name,int uid);
// public Photo findPhotoByName2(Map<String, Object> v);
public Photo findPhotoByName2(Photo photo);
public List<Photo> findPhotos();
}
<?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.bjsxt.dao.PhotoDao">
<!-- Id :dao中的方法名字 parameterType:方法中传参的类型 -->
<insert id="savePhoto" parameterType="Photo"> <!-- name 代表方法中第一个参数的name属性值 -->
insert into t_photo (name,uploadDate) values (#{name},#{uploadDate})
</insert>
<!-- java中基本数据类型和string 都不需指定类型 parameterType="int" 不要-->
<select id="getPhotoById" resultType="Photo"> <!-- dsfsfs 代表方法中第一个参数的值 -->
select * from t_photo where id=#{dsfsfs}
</select>
<!-- #{和方法中参数的名字无关} {表示第n个参数的值或者其属性的值,或者其key的值}-->
<select id="findPhotoByName" resultType="Photo"> <!-- {}可以使用下标表示n -->
select * from t_photo where name=#{0} and uid=#{1}
</select>
<select id="findPhotoByName2" resultType="Photo"> <!-- {}: map中的key -->
select * from t_photo where name=#{name} and uid=#{uid}
</select>
<!-- resultType表示sql语句执行之后返回的数据类型 -->
<select id="findPhotos" resultType="Photo">
select * from t_photo
</select>
</mapper>
<select id="findPhotoName" resultType="map">
select p.name as pn,a.name as an from t_photo as p join t_album a on p.aid=a.id
</select>
public List<Photo> findPhotos();
public List<Album> findAlbum(List<Integer> ids);
//list<?> 接口里面的 因为查找包含条件不同,所以需要动态
<select id="findAlbum" resultType="Album">
select * from t_album where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<select id="findAlbum2" resultType="Album"><!-- 整数不能当成int,而是Integer -->
select * from t_album
<where>
<if test="name!=null">
name=#{name}
</if>
<if test="name !=null and uid!=null">
and
</if>
<if test="uid!=null">
uid=#{uid}
</if>
</where>
</select>
就是如果where 后面有东东的话 就会输出where + 后面的东东 否则 就不输出
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<trim prefix="where" prefixOverrides="and |or">
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
or owner = #{owner}
</if>
</trim>
</select>
@Test
public void test3()throws Exception{
String path="mybatis.xml"; //配置文件(根目录下的)
Reader reader =Resources.getResourceAsReader(path);
SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(reader);
SqlSession session =sessionFactory.openSession();
AlbumDao albumDao = session.getMapper(AlbumDao.class); //调用的接口(dao里面的java类)
try{
Map<String, Object> map =new HashMap<String, Object>();
map.put("name", "相册2");
// map.put("uid", 1);
System.out.println(albumDao.findAlbum2(map).size());
//这个是调用方法,返回的是sql执行之后的放回结果}catch (Exception e) {
session.rollback();
e.printStackTrace();
}finally{
session.close();
}
}
@Update("update t_album set prePath=#{0} ,imgCount=#{1} where id=#{2}")
public void updateAlbum(String prePath,long count,int id);
@Select("select * from t_photo where id=#{dsfds}")
@ResultType(Photo.class)
public Photo getPhotoById(int id);
原文地址:http://blog.csdn.net/xiaohangblog/article/details/39897831