| 注解 | 目标 | 对应的XML标签 |
| @CacheNamespace | 类 | <cache> |
| @CacheNamespaceRef | 类 | <cacheRef> |
| @Results | 方法 | <resultMap> |
| @Result | 方法 |
<result> <id> |
| @One | 方法 | <association> |
| @Many | 方法 | <collection> |
| @Insert @Update @Delete | 方法 | <insert> <update> <delete> |
| @InsertProvider @UpdateProvider @DeleteProvider @SelectProvider | 方法 | <insert> <update> <delete> <select> 允许创建动态SQL |
| @Param | 参数 | N/A |
| @Options | 方法 | 映射语句的属性 |
| @select | 方法 | <select> |
//添加作者
@Insert("Insertinto Author(username,password,email,address,phone) " +
"values(#{username},#{password},#{email},#{address},#{phone})")
@Options(useGeneratedKeys=true,keyProperty="authId",flushCache= false, timeout = 10000)
public voidaddAuthor(Author author);
//删除作者
@Delete("deletefrom author where id = #{id}")
@Options(flushCache= false, timeout = 10000)
public voiddeleteAuthor(@Param("id") int id);
sessionFactory.getConfiguration().addMapper(TestInteger.class);
//查询所有作者信息
@Select("select * from author")
@Options(flushCache = false, timeout = 10000,useCache=true)
@Results(
value = {
@Result(id=true,column="id",property="id"),
@Result(property="username",column="username"),
@Result(property="password",column="password"),
@Result(property="email",column="email"),
@Result(property="address",column="address"),
@Result(property="phone",column="phone")
}
)
public List<Author> findAuthors();
//查询某作者信息
@Select("select * from author where id =#{id}")
@Options(flushCache = false, timeout =10000,useCache=true)
@Results(
value = {@Result(id=true,column="id",property="id"),
@Result(property="username",column="username"),
@Result(property="password",column="password"),
@Result(property="email",column="email"),
@Result(property="address",column="address"),
@Result(property="phone",column="phone")
}
)
public Author findAuthorById(@Param("id") intid);
//每行记录是一个hashmap
<resultMaptype="java.util.HashMap" id="selfMap">
<resultproperty="n" column="city_name" />
...............
</resultMap>
@Select("select a.id,b.name,c.state from...........")
@ResultMap(value="selfMap")
public List<HashMap> sel();//注意,返回的是List集合
package com.obtk.dao;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.obtk.entitys.StudentEntity;
public interface IStudentDao {
@Insert("insert into Student(stuName,gender,age,address,deptIdd)"+
"values(#{stuName},#{gender},#{age},#{address},#{deptIdd})")
@Options(useGeneratedKeys=true,keyProperty="stuId")
int saveOne(StudentEntity stu);
@Select("select * from Student where stuId=#{stuId}")
@Results(
//只要配置和列名不一致的属性
value={
@Result(column="gender",property="sex")
}
)
StudentEntity queryById(Integer stuId);
@Select("select * from Student where gender=#{qqq} and address=#{area}")
@Results(
//只要配置和列名不一致的属性
value={
@Result(column="gender",property="sex")
}
)
List<StudentEntity> queryByMany(HashMap theMap);
//万能关联注解配置
@Select("select * from student s inner join department d"
+" on s.deptIdd=d.deptId"
+" where s.gender=#{sex}"
+" and d.departName=#{deptName}")
List<HashMap> queryByQnn(HashMap theMap);
}
package com.obtk.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.obtk.dao.IStudentDao;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class AnnoSelectOne {
public static void main(String[] args) {
SqlSession session=null;
SqlSessionFactory factory=null;
try {
session=MybatisUtil.getSession();
factory=MybatisUtil.getFactory();
//把接口里面的sql配置和核心配置文件进行关联
factory.getConfiguration().addMapper(IStudentDao.class);
IStudentDao stuDao=session.getMapper(IStudentDao.class);
StudentEntity stu=stuDao.queryById(129);
System.out.println(stu.getStuName()+","+stu.getSex()
+","+stu.getAddress()+","+stu.getStuId());
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
package com.obtk.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.obtk.dao.IStudentDao;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class AnnoSelectMany {
public static void main(String[] args) {
SqlSession session=null;
SqlSessionFactory factory=null;
try {
session=MybatisUtil.getSession();
factory=MybatisUtil.getFactory();
//把接口里面的sql配置和核心配置文件进行关联
factory.getConfiguration().addMapper(IStudentDao.class);
IStudentDao stuDao=session.getMapper(IStudentDao.class);
HashMap paramMap=new HashMap();
paramMap.put("qqq", "男");
paramMap.put("area", "学生宿舍");
List<StudentEntity> stuList=stuDao.queryByMany(paramMap);
for(StudentEntity stu :stuList){
System.out.println(stu.getStuName()+","+stu.getSex()
+","+stu.getAddress()+","+stu.getStuId());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
package com.obtk.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.obtk.dao.IStudentDao;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class AnnoSaveTest {
public static void main(String[] args) {
SqlSession session=null;
SqlSessionFactory factory=null;
try {
session=MybatisUtil.getSession();
factory=MybatisUtil.getFactory();
//把接口里面的sql配置和核心配置文件进行关联
factory.getConfiguration().addMapper(IStudentDao.class);
IStudentDao stuDao=session.getMapper(IStudentDao.class);
StudentEntity stu=new StudentEntity("testC#",
"男", 21, "冥王星");
stu.setDeptIdd(10);
int result=stuDao.saveOne(stu);
session.commit();
System.out.println("保存成功:"+stu.getStuId());
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
package com.obtk.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.obtk.dao.IStudentDao;
import com.obtk.entitys.StudentEntity;
import com.obtk.utils.MybatisUtil;
public class AnnoJoinQnn {
public static void main(String[] args) {
SqlSession session=null;
SqlSessionFactory factory=null;
try {
//4.得到session
session=MybatisUtil.getSession();
factory=MybatisUtil.getFactory();
//把接口里面的sql配置和核心配置文件进行关联
factory.getConfiguration().addMapper(IStudentDao.class);
IStudentDao stuDao=session.getMapper(IStudentDao.class);
HashMap paramMap=new HashMap();
paramMap.put("sex", "男");
paramMap.put("deptName", "计算机系");
//5.执行语句
List<HashMap> stuList=stuDao.queryByQnn(paramMap);
for(HashMap theObj : stuList){
System.out.println(theObj.get("stuId")+","+theObj.get("gender")
+","+theObj.get("stuName")+","+theObj.get("departName"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
MybatisUtil.closeSession();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有