maybats的mapper动态代理多个数据操作数据库
mapper.xml代码:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- mapper.xml的父标签 namespac
·
mapper.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.xml的父标签 namespace+id==类名+方法名 -->
<!--
如果想让接口和xml关联,必须满足如下配置:
1.namespace的值是接口的全路径
2.xml中标签的id必须关联dao接口中的方法名
-->
<mapper namespace="com.zx.mybatis.mapper.StudentMapper">
<insert id="saveStudent" parameterType="Student">
<!-- 注意:sql结尾不要加分号 -->
insert into STUDENT (ID,USER_NAME,AGE,SCORE) values (#{id}, #{name}, #{age}, #{score})
</insert>
<!--
parameterType:参数类型
resultType:返回值类型
-->
<select id="selectStu" parameterType="_int" resultType="Student">
<!-- 注意:sql结尾不要加分号 -->
<!-- 当只有一个参数时,参数名可以任意写 -->
select id, name, age, score from student where id = #{iidd}
</select>
<update id="gxStudent" parameterType="Student">
update student set name=#{name}, age=#{age}, score=#{score} where id=#{id}
</update>
<delete id="scStudent" parameterType="_int">
delete from student where id=#{id}
</delete>
<!-- 查所有List,resultType中的返回值类型写List中的泛型 -->
<select id="selectListStudent" resultType="Student">
select id, name, age, score from student
</select>
<!-- 查所有Map,resultType中的返回值类型写Map中value的泛型 -->
<select id="selectMapStudent" resultType="Student">
select id, name, age, score from student
</select>
<!-- 查所有Map,resultType中的返回值类型写Map中value的泛型 -->
<!-- 模糊查询,写死字符串传参时只识别${}的语法 -->
<select id="mhSelectStudent" parameterType="string" resultMap="myStudent">
<!-- select * from student where name like '${abc}%' -->
<!-- 模糊查询,动态传参时可识别#{}的语法,防止sql注入 -->
<!-- #{}是占位,${}是拼接 -->
<!-- 如果属性名和字段名不一致,解决方法1:在sql中给字段起别名 -->
select id, user_name, age, score from student where user_name like concat(#{abc}, '%')
<!-- 如果属性名和字段名不一致,解决方法2:使用resultMap进行映射 -->
</select>
<!-- 自定义结果集映射 -->
<resultMap id="myStudent" type="Student">
<!-- 主键的映射关系,column:字段名,property:属性名 -->
<id column="id" property="id"/>
<!-- 非主键关系映射用result -->
<result column="user_name" property="name"/>
<result column="age" property="age"/>
<result column="score" property="score"/>
</resultMap>
<!-- select标签中:parameterType可有可无,但resultType和resultMap必须有一个 -->
<select id="selectStuByAgeAndScore" resultMap="myStudent">
select id, user_name, age, score from student where age = #{age} and score = #{score}
</select>
</mapper>
dao接口代码:
package com.zx.mybatis.mapper;
import java.util.List;
import java.util.Map;
import com.zx.mybatis.entity.Student;
public interface StudentMapper {
/**
* 保存用户
* @param stu
* @return
*/
public int saveStudent(Student stu);
/**
*查询用户
* @param id
* @return
*/
public Student selectStu(int id);
/**
* 通过年龄和分数筛选学生
* @param age
* @param score
* @return
*/
public List<Student> selectStuByAgeAndScore(Map<String,Object> map);
}
java测试代码:
package com.zx.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.zx.mybatis.entity.Student;
import com.zx.mybatis.mapper.StudentMapper;
import com.zx.mybatis.util.MybatisUtil;
public class Test1 {
@Test
public void Test01(){
//主配置文件的位置
String resource = "mybatis.xml";
//加载对应路径下的配置文件到流中
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
//sqlSession工厂:用来创建sqlSession
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
/*
* 调用sqlSession的新增方法
* statement:mapper.xml的位置(namespace+id)
* parameter:参数
* result:这条sql影响的条目数
*/
int result = sqlSession.insert("a.b.c.xzStudent", new Student(11, "李四", 23, 90.01));
//提交
sqlSession.commit();
System.out.println(result);
//释放资源
sqlSession.close();
}
@Test
public void Test02(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
Student stu = sqlSession.selectOne("a.b.c.cxStudent", 1);
System.out.println(stu);
sqlSession.close();
}
@Test
public void Test03() {
//update
SqlSession sqlSession = MybatisUtil.getSqlSession();
int result = sqlSession.update("a.b.c.gxStudent", new Student(11, "李四123", 23, 90.01));
System.out.println(result);
sqlSession.commit();
sqlSession.close();
}
@Test
public void Test04() {
//delete
SqlSession sqlSession = MybatisUtil.getSqlSession();
int result = sqlSession.delete("a.b.c.scStudent", 11);
System.out.println(result);
sqlSession.commit();
sqlSession.close();
}
@Test
public void Test05() {
//selectList
SqlSession sqlSession = MybatisUtil.getSqlSession();
List<Student> students = sqlSession.selectList("a.b.c.selectListStudent");
for(Student s : students) {
System.out.println(s);
}
sqlSession.close();
}
@Test
public void Test06() {
//selectMap
SqlSession sqlSession = MybatisUtil.getSqlSession();
Map<Integer, Student> map = sqlSession.selectMap("a.b.c.selectMapStudent", "age");
//遍历map
Set<Map.Entry<Integer, Student>> entrySet = map.entrySet();
for(Map.Entry<Integer, Student> entry : entrySet) {
System.out.println("key:"+entry.getKey()+",value:"+entry.getValue().toString());
}
sqlSession.close();
}
/**
* 模糊查询
*/
@Test
public void Test07() {
//selectMap
SqlSession sqlSession = MybatisUtil.getSqlSession();
List<Student> students = sqlSession.selectList("a.b.c.mhSelectStudent", "李");
for(Student s : students) {
System.out.println(s);
}
sqlSession.close();
}
/**
* mapper的动态代理(自动为我们生成StudentDaoImpl)
*/
@Test
public void Test08() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
//使用mapper动态代理技术生成对应接口的实现类
StudentMapper sd = sqlSession.getMapper(StudentMapper.class);
sd.saveStudent(new Student(15, "李思思", 23, 90.01));
sqlSession.commit();
System.out.println(sd);
}
@Test
public void Test09() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
//使用mapper动态代理技术生成对应接口的实现类
StudentMapper sd = sqlSession.getMapper(StudentMapper.class);
int age = 23;
double score = 90.01;
Map<String, Object> map = new HashMap<>();
map.put("age", age);
map.put("score", score);
//多参数如何传递
List<Student> stus = sd.selectStuByAgeAndScore(map);
for(Student s : stus) {
System.out.println(s);
}
sqlSession.close();
}
}
java工具类代码:
package com.zx.mybatis.util;
import java.io.IOException;
import java.io.InputStream;
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 MybatisUtil {
private static String resource = "mybatis.xml";
private static SqlSessionFactory sqlSessionFactory = null;
/**
* 获取sqlSession
* @return
*/
public static SqlSession getSqlSession() {
if(sqlSessionFactory==null) {
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 调用sqlSession工厂创建一个SqlSession
* 它有重载的方法,用来控制事务是否自动提交
* 默认是不自动进行事务提交的
*/
return sqlSessionFactory.openSession();
}
}
jdbc.propertes代码:
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=zhangyi
jdbc.password=123456
java实体类代码:
package com.zx.mybatis.entity;
/**
* 学生类
* @author zhangyi
*
*/
public class Student {
private int id;
private String name;
private int age;
private double score;
public Student() {
super();
}
public Student(int id, String name, int age, double score) {
super();
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";
};
}
更多推荐
已为社区贡献2条内容
所有评论(0)