mybatis的配置以及连接MySQL数据库进行复杂数据的select和insert
Posted 名字真的很急用
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis的配置以及连接MySQL数据库进行复杂数据的select和insert相关的知识,希望对你有一定的参考价值。
废话不多说,直接上代码
第一步肯定是先创建直接的数据库mybatis,然后建立自己的Student,然后进行数据库的数据插入,一便我们后续的进行操作。
INSERT INTO Student VALUES ('1','joy','18','13352598223');
INSERT INTO Student VALUES ('2','tom','16','13352008223');
INSERT INTO Student VALUES ('3','jack','20','18852598223');
SELECT * FROM Student;
第二步,进行我们mybatis-config.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>
<settings>
<setting value="true" name="cacheEnabled"/>
<!--开启二级缓存-->
</settings>
<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:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
第三步,定义我们自己的pojo类,也就是Student对象。
package com.po;
public class Student {
private int id;
private String name;
private int age;
private String phone;
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", phone=" + phone + "]";
}
}
第四部进行我们的StudentMapper.xml以及自己定义的方法接口。
package com.mapper;
import com.po.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface StudentMapper {
//一个参数
public Student selectOneStudent(Integer id);
//多个参数
public Student selectTwoStudent(@Param("id") Integer id,@Param("name") String name);
//参数类型 pojo
public Student selectThreeeStudent(Student student);
//参数类型 map
public Student selectMapStudent(Map map);
//参数类型 复杂类型
public Student selectFzStudent(@Param("id") Integer id, @Param("stu") Student student);
//参数类型 集合类型
public Student selectListStudent(@Param("a") List list);
public Integer insertStudent(Student student);
}
使用getMapper(Class type)方法。比较快捷,自己定义
<?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.mapper.StudentMapper">
<select id="findStudentById" parameterType="Integer"
resultType="com.po.Student">
select * from Student where id =#{id}
</select>
<select resultType="com.po.Student" id="selectOneStudent"> select id,name,age,phone from student where id = #{id} </select>
<!--多个参数-->
<insert id="insertStudent">insert into Student values(default,#{name},#{age},#{phone})</insert>
<select resultType="com.po.Student" id="selectTwoStudent"> select id,name,age,phone from student where id = #{id} and name = #{name} </select>
<!-- 参数类型:pojo -->
<select resultType="com.po.Student" id="selectThreeeStudent"> select id,name,age,phone from student where id = #{id} and name = #{name} </select>
<!-- 参数类型:map <key : value>-->
<select resultType="com.po.Student" id="selectMapStudent"> select id,name,age,phone from student where id = #{id} and name = #{name} </select>
<!-- 参数类型: 复杂类型 -->
<select resultType="com.po.Student" id="selectFzStudent"> select id,name,age,phone from student where id = #{id} and name = #{stu.name} </select>
<!-- 参数类型: 集合 -->
<select resultType="com.po.Student" id="selectListStudent"> select id,name,age,phone from student where id = #{a[0]} and name = #{a[1]} </select>
</mapper>
最后就是我们自己的测试类。
package com.test;
import com.po.Student;
import com.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import Utils.MybatisUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class test {
public void test1() {
SqlSession session = MybatisUtils.getSession();
Student student = session.getMapper(StudentMapper.class).selectOneStudent(1);
System.out.println(student);
session.close();
}
public void test2() {
SqlSession session = MybatisUtils.getSession();
Student student = session.getMapper(StudentMapper.class).selectTwoStudent(2,"tom");
System.out.println(student);
session.close();
}
public void test3() {
SqlSession session = MybatisUtils.getSession();
Student student = new Student();
student.setId(2);
student.setName("tom");
Student stu = session.getMapper(StudentMapper.class).selectThreeeStudent(student);
System.out.println(stu);
session.close();
}
//参数类型 map
public void test4() {
SqlSession session = MybatisUtils.getSession();
Map map = new HashMap();
map.put("id",2);
map.put("name","tom");
Student stu = session.getMapper(StudentMapper.class).selectMapStudent(map);
System.out.println(stu);
session.close();
}
//参数类型 复杂类型
public void test5() {
SqlSession session = MybatisUtils.getSession();
Integer id = 2;
Student student = new Student();
student.setName("tom");
Student stu = session.getMapper(StudentMapper.class).selectFzStudent(id,student);
System.out.println(stu);
session.close();
}
//参数类型 集合
public void test6() {
SqlSession session = MybatisUtils.getSession();
List list = new ArrayList();
list.add(2);
list.add("tom");
Student stu = session.getMapper(StudentMapper.class).selectListStudent(list);
System.out.println(stu);
session.close();
}
public void test11() {
SqlSession session = MybatisUtils.getSession();
long a = System.nanoTime();//记录计算机运行的纳秒 秒 毫秒 微妙 纳秒
Student student = session.getMapper(StudentMapper.class).selectOneStudent(1);
long b = System.nanoTime();
System.out.println(b-a);
Student stu = new Student();
stu.setName("张三丰1");
stu.setAge(23);
stu.setPhone("1555555555");
Integer temp = session.getMapper(StudentMapper.class).insertStudent(stu);
if(temp > 0){
System.out.println("save成功");
}else {
System.out.println("save失败");
}
long a1 = System.nanoTime();
Student student1 = session.getMapper(StudentMapper.class).selectOneStudent(1);
long b1 = System.nanoTime();
System.out.println(b1-a1);
System.out.println(student);
System.out.println(student1);
session.commit();
session.close();
}
public static void main(String[] args) {
test t = new test();
//t.test1();
//t.test2();
//t.test3();
//t.test4();
//t.test5();
//t.test6();
t.test11();
/* Map map = new HashMap();
map.put("name1","jim1");
map.put("name2","jim2");
map.put("name3","jim3");
System.out.println(map.get("name1"));
System.out.println(map.get("name2"));
System.out.println(map.get("name3"));*/
}
}
欢迎指正。
以上是关于mybatis的配置以及连接MySQL数据库进行复杂数据的select和insert的主要内容,如果未能解决你的问题,请参考以下文章
mybatis连接mysql数据库对数据增,删,改以及查询所有