<?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>
<properties resource="jdbc.properties"/>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<typeAliases>
<package name="com.powernode.mybatis.POJO"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.powernode.mybatis.Mapper"/>
</mappers>
</configuration>
<?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>
<properties resource="jdbc.properties"/>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<typeAliases>
<package name="com.powernode.mybatis.POJO"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.powernode.mybatis.Mapper"/>
</mappers>
</configuration>
<?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.powernode.mybatis.Mapper.StudentMapper">
<!-- parameterType的作用是告诉Mybatis框架我们的属性是什么类型,但是大部分情况下自动推断后我们就不用写了-->
<!-- parameterType其实可以写别名,Mybatis内置了很多别名-->
<select id="selectById" resultType="Student" parameterType="long">
select * from t_student where id = #{id,javaType=Long,jdbcType=BIGINT};
</select>
<select id="selectByName" resultType="Student" parameterType="String">
select * from t_student where name = #{name,javaType=String,jdbcType=VARCHAR};
</select>
<select id="selectByBirth" resultType="Student" parameterType="Date">
select * from t_student where birth = #{birth,javaType=Date,jdbcType=DATE};
</select>
<select id="selectBySex" resultType="Student" parameterType="Character">
select * from t_student where sex = #{sex,javaType=Character,jdbcType=CHAR}
</select>
<insert id="insertStudentsByMap">
insert into t_student(id,name,age,sex,birth,height) values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高});
</insert>
<insert id="InsertStudentByPOJO" parameterType="com.powernode.mybatis.POJO.Student">
insert into t_student(id,name,age,sex,birth,height) values(null,#{name},#{age},#{sex},#{birth},#{height});
</insert>
</mapper>
<?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.powernode.mybatis.Mapper.StudentMapper">
<!-- parameterType的作用是告诉Mybatis框架我们的属性是什么类型,但是大部分情况下自动推断后我们就不用写了-->
<!-- parameterType其实可以写别名,Mybatis内置了很多别名-->
<select id="selectById" resultType="Student" parameterType="long">
select * from t_student where id = #{id,javaType=Long,jdbcType=BIGINT};
</select>
<select id="selectByName" resultType="Student" parameterType="String">
select * from t_student where name = #{name,javaType=String,jdbcType=VARCHAR};
</select>
<select id="selectByBirth" resultType="Student" parameterType="Date">
select * from t_student where birth = #{birth,javaType=Date,jdbcType=DATE};
</select>
<select id="selectBySex" resultType="Student" parameterType="Character">
select * from t_student where sex = #{sex,javaType=Character,jdbcType=CHAR}
</select>
<insert id="insertStudentsByMap">
insert into t_student(id,name,age,sex,birth,height) values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高});
</insert>
<insert id="InsertStudentByPOJO" parameterType="com.powernode.mybatis.POJO.Student">
insert into t_student(id,name,age,sex,birth,height) values(null,#{name},#{age},#{sex},#{birth},#{height});
</insert>
</mapper>
package com.powernode.mybatis.Mapper;
import com.powernode.mybatis.POJO.Student;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public interface StudentMapper
{
//通过POJO保存学生信息
int InsertStudentByPOJO(Student student);
//通过Map保存学生信息
int insertStudentsByMap(Map<String, Object> map);
List<Student> selectById(Long id);
List<Student> selectByName(String name);
List<Student> selectByBirth(Date birth);
List<Student> selectBySex(Character sex);
}
package com.powernode.mybatis.Mapper;
import com.powernode.mybatis.POJO.Student;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public interface StudentMapper
{
//通过POJO保存学生信息
int InsertStudentByPOJO(Student student);
//通过Map保存学生信息
int insertStudentsByMap(Map<String, Object> map);
List<Student> selectById(Long id);
List<Student> selectByName(String name);
List<Student> selectByBirth(Date birth);
List<Student> selectBySex(Character sex);
}
package com.powernode.mybatis.Test;
import com.powernode.mybatis.Mapper.StudentMapper;
import com.powernode.mybatis.POJO.Student;
import com.powernode.mybatis.Utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test
{
@org.junit.Test
public void Test()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectById(1L);
students.forEach(student -> {
System.out.println(student);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelect()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> list = mapper.selectByName("李四");
list.forEach(l ->{
System.out.println(l);
});
}
@org.junit.Test
public void TestDate() throws Exception
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birth = simpleDateFormat.parse("2000-10-11");
List<Student> list = mapper.selectByBirth(birth);
list.forEach(l -> {
System.out.println(l);
});
}
@org.junit.Test
public void TestSEX()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> list = mapper.selectBySex('男');
list.forEach(te -> {
System.out.println(te);
});
}
@org.junit.Test
public void TestInsertMap() throws Exception
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("姓名","王五");
map.put("年龄",20);
map.put("身高",1.82);
map.put("性别",'男');
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse("2001-08-14");
map.put("生日",date);
mapper.insertStudentsByMap(map);
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestInsertByPOJO()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student(null,"展会",20,1.8,new Date(),'女');
int i = mapper.InsertStudentByPOJO(student);
System.out.println(i);
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}
}
package com.powernode.mybatis.Test;
import com.powernode.mybatis.Mapper.StudentMapper;
import com.powernode.mybatis.POJO.Student;
import com.powernode.mybatis.Utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test
{
@org.junit.Test
public void Test()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectById(1L);
students.forEach(student -> {
System.out.println(student);
});
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestSelect()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> list = mapper.selectByName("李四");
list.forEach(l ->{
System.out.println(l);
});
}
@org.junit.Test
public void TestDate() throws Exception
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birth = simpleDateFormat.parse("2000-10-11");
List<Student> list = mapper.selectByBirth(birth);
list.forEach(l -> {
System.out.println(l);
});
}
@org.junit.Test
public void TestSEX()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> list = mapper.selectBySex('男');
list.forEach(te -> {
System.out.println(te);
});
}
@org.junit.Test
public void TestInsertMap() throws Exception
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("姓名","王五");
map.put("年龄",20);
map.put("身高",1.82);
map.put("性别",'男');
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse("2001-08-14");
map.put("生日",date);
mapper.insertStudentsByMap(map);
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}
@org.junit.Test
public void TestInsertByPOJO()
{
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = new Student(null,"展会",20,1.8,new Date(),'女');
int i = mapper.InsertStudentByPOJO(student);
System.out.println(i);
sqlSession.commit();
SqlSessionUtil.close(sqlSession);
}
}
还没有评论,来说两句吧...