mybatis所需pom文件内容以及配置文件
官方网站http://www.mybatis.org/mybatis-3/zh/index.html
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.yujian</groupId>
<artifactId>mybatisdemo3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency> </dependencies>
<!--下面是防止src/main/java下面的东西无法加载到resources下面(若是把db.properties和mybatis-config.xml直接放在resources下面,则不需要)-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project> 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>
<properties resource="db.properties"></properties>
<!--导入参数来源-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--用于生成可视的sql命令-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings><!--配置驼峰命名法必须在properties后面,有顺序,ctr然后点configuration查看顺序-->
<typeAliases>
<!--<typeAlias type="com.yujian.pojo.Student" alias="s2"></typeAlias>-->
<!--如果用上面这种,则每个pojo里面的类都需要加入进来写别名-->
<package name="com.yujian.pojo"></package>
<!--如果上面这种,则自动扫描包pojo下的注解类的别名-->
</typeAliases>
<!--environments可以写多个,然后通过写id,在StudentMapper.xml下的sql语句中
加入大baseid即如:<select id="findeStudentList" resultMap="r1" databaseId="development">
select * from student ;
</select>-->
<environments default="development">
<environment id="development"><!--环境-->
<transactionManager type="JDBC"/><!--事务管理器-->
<dataSource type="POOLED"><!--数据源-->
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="com/yujian/model/StudentMapper.xml"/>
<mapper resource="com/yujian/model/ClassInfoMapper.xml"/>-->
<!--设置反射来源-->
<!--或者用下面这种,可以自动查询包下所有文件-->
<package name="com.yujian.model"></package>
<!--<mapper class="com.yujian.model.StudentMapper"></mapper>这种也可以-->
</mappers>
</configuration> db.properties
#mysql
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/school?useSSL=false
mysql.username=root
mysql.password=root MybatisHelper.class
package com.yujian.util; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException;
import java.io.InputStream; public class MybatisHelper {
private static SqlSessionFactory sqlSessionFactory=null;
public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null) {
try {
/*InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);*/
InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return sqlSessionFactory;
}
} studentsMapper.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 namespace="com.yujian.model.StudentMapper">
<!--<select id="findeStudentList" resultType="com.yujian.pojo.Student">
select * from student ;
</select>-->
<resultMap id="r1" type="s2">
<!-- <id property="" column="" />-->
<!--<result column="stuid" property="stuId"/>-->
<!--上面的type=或者可以用注解的别名-->
<!--两种方法type="com.yujian.pojo.Student"一般用这种-->
<!--如果属性名和列的字段名不匹配,通过resultmap可以属性和字段进行匹配-->
<result column="stuName" property="stuName1"></result>
<result column="classid" property="classInfo.classId"></result>
<result property="classInfo.className" column="classname"/>
</resultMap>
<!--如果使用association标签,name其他映射会失效,需要加上automapping=true-->
<resultMap id="r2" type="com.yujian.pojo.Student" autoMapping="true">
<result column="stuName" property="stuName1"></result>
<!--<association property="classInfo" javaType="com.yujian.pojo.ClassInfo">
<result column="classid" property="classId"></result>
<result column="classname" property="className"></result>
</association>-->
<!--或者下面这种-->
<association property="classInfo" javaType="com.yujian.pojo.ClassInfo"
select="com.yujian.model.ClassInfoMapper.findListClassInfoById" column="classid">
</association>
</resultMap>
<select id="findAssoListStudent" resultMap="r2">
select s.*,c.* from student s inner join classinfo c on s.classId=c.classId ;
</select> <!--下面如果用resultType="s2"或者"com.yujian.pojo.Student",则会造成属性名和列字段名不匹配,找不到-->
<!--<select id="findeStudentList" resultMap="r1">
select s.*,c.* from student s inner join classinfo c on s.classId=c.classId ;
</select>-->
<!--<select id="findStudentById" resultMap="r1" parameterType="int">
select s.*,c.* from student s inner join classinfo c on s.classId=c.classId where stuid=#{stuId};
</select>-->
<insert id="insertStudent" parameterType="com.yujian.pojo.Student">
insert into student(stuId,stuName,stuSex,stuBirth,classId) values (null ,#{stuName1},#{stuSex},now(),#{classInfo.classId});
</insert>
<update id="updateStudent" parameterType="com.yujian.pojo.Student">
update student set stuName=#{stuName1},stuSex=#{stuSex},classId=#{classInfo.classId},stuBirth=#{stuBirth} where stuId=#{stuId};
</update>
<delete id="deleteStudent" parameterType="int">
delete from student where stuId=#{stuId};
</delete>
<!--<resultMap id="result" type="com.yujian.pojo.Student">
<result column="stuname" property="stuName1"></result>
</resultMap>
<select id="findStuLike" resultMap="result">
select * from student where stuName like #{param1} and stuSex like #{param2} and stuId=#{param3}
</select>-->
<resultMap id="result" type="com.yujian.pojo.Student">
<result column="stuname" property="stuName1"></result>
</resultMap>
<select id="findStuMap" resultMap="result">
select * from student where stuName like #{stuName} and stuSex like #{stuSex} and stuId=#{stuId}
</select> </mapper>
StudentMapper.class
package com.yujian.model; import com.yujian.pojo.Student;
import org.apache.ibatis.annotations.*; import java.util.List;
import java.util.Map; public interface StudentMapper {
@Select("select s.*,c.* from student s inner join classinfo c on s.classid=c.classid")
@Results(id = "student",value = {
@Result(column = "stuName",property = "stuName1"),
/*@Result(column = "classId",property = "classInfo.classId"),
@Result(column = "className",property = "classInfo.className"),*/
@Result(property = "classInfo",column = "classId",one=@One(select = "com.yujian.model.ClassInfoMapper.findListClassInfoById"))
/* @Result(column = "stuId",property = "stuId"),
@Result(column = "stuBirth",property = "stuBirth"),
@Result(column = "stuSex",property = "stuSex")
列不需要字段写全,只要不同名字段属性,给一一匹配就好
*/ })
List<Student> findeStudentList();
@Select("select s.*,c.* from student s inner join classinfo c on s.classid=c.classid")
@Results(id = "student2",value = {
@Result(column = "stuName",property = "stuName1"),
@Result(column = "className",property = "classInfo.className") ,
@Result(column = "classId",property = "classInfo.classId")
})
List<Student> findStudentList();
@Select("select s.*,c.* from student s inner join classinfo c on s.classid=c.classid" +
" where s.stuid=#{stuId}")
/*@ResultMap("com.yujian.model.StudentMapper.r1")*/
@ResultMap("student2") Student findStudentById(int stuId);
void insertStudent(Student student);
void updateStudent(Student student);
void deleteStudent(int stuId);
@Select("select s.*,c.* from student s inner join classinfo c on s.classid=c.classid" +
" where s.classId=#{classId}")
/*@ResultMap("com.yujian.model.StudentMapper.r1")*/
/*@Results({
@Result(column = "stuName",property = "stuName1")
})*/
@Results(id = "student3",value = {
@Result(column = "stuName",property = "stuName1"),
@Result(column = "className",property = "classInfo.className") ,
@Result(column = "classId",property = "classInfo.classId")
})
List<Student> findStudentListByClassId(int classId);
List<Student> findAssoListStudent();
@Select("select * from student where stuName like #{stuName} and stuSex like #{stuSex} and stuId=#{stuId}")
@Results({
@Result(column = "stuName",property = "stuName1")
})
List<Student> findStuLike(@Param("stuName") String stuName,@Param("stuSex") String stuSex,@Param("stuId") int stuId);
List<Student> findStuMap(Map map);
@Select("select * from student where stuName like #{param1} and stuSex like #{param2} and stuId=#{param3}")
@Results({
@Result(column = "stuName",property = "stuName1")
})
List<Student> findStuLike2(String stuName,String stuSex,int stuId);
}
测试文件
import com.yujian.model.ClassInfoMapper;
import com.yujian.model.StudentMapper;
import com.yujian.pojo.ClassInfo;
import com.yujian.pojo.Student;
import com.yujian.util.Dateformat;
import com.yujian.util.MybatisHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test; import java.util.HashMap;
import java.util.List;
import java.util.Map; public class StudentTest {
@Test
public void testFindStudentList(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory(); SqlSession sqlSession=sqlSessionFactory.openSession();
try {
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
List<Student> students=studentMapper.findeStudentList();
System.out.println(students.size());
System.out.println(students.get(0).getStuName1());
System.out.println(students.get(0).getClassInfo().getClassName());
System.out.println(students.get(0).getStuSex()); /*sqlSession.commit();*/
/* sqlSession.rollback();*/
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void testFindStudent(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory();
SqlSession sqlSession=sqlSessionFactory.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
try {
Student student=studentMapper.findStudentById(2);
System.out.println(student.getClassInfo().getClassName());
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void testinsertStudent(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory();
SqlSession sqlSession=sqlSessionFactory.openSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
ClassInfoMapper classInfoMapper=sqlSession.getMapper(ClassInfoMapper.class); try {
Student student=new Student();
student.setStuName1("李四");
student.setStuSex(1);
ClassInfo classInfo=null;
classInfo=classInfoMapper.findListClassInfoById(1);
student.setClassInfo(classInfo); studentMapper.insertStudent(student);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} }
@Test
public void testupdateStudent(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory();
SqlSession sqlSession=sqlSessionFactory.openSession(); StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
ClassInfoMapper classInfoMapper=sqlSession.getMapper(ClassInfoMapper.class);
Dateformat format=new Dateformat(); try {
Student student=studentMapper.findStudentById(2);
student.setStuName1("王五");
student.setStuSex(0);
String birth="1997-03-30";
student.setStuBirth(format.format(birth));
ClassInfo classInfo=null;
classInfo=classInfoMapper.findListClassInfoById(2);
student.setClassInfo(classInfo); studentMapper.updateStudent(student);
sqlSession.commit(); } catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void testDeleteStudent(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory();
SqlSession sqlSession=sqlSessionFactory.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); try {
studentMapper.deleteStudent(3);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} }
@Test
public void testfindStudentListByClassId(){
SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory();
SqlSession sqlSession=sqlSessionFactory.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); try {
List<Student> students=studentMapper.findStudentListByClassId(1);
System.out.println(students.get(0).getClassInfo().getClassName());
System.out.println(students.get(0).getStuName1());
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} }
@Test
public void testFindAssoListStudent(){ SqlSessionFactory sqlSessionFactory=MybatisHelper.getSqlSessionFactory(); SqlSession sqlSession=sqlSessionFactory.openSession();
try {
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
List<Student> students=studentMapper.findAssoListStudent();
System.out.println(students.size());
System.out.println(students.get(0).getStuName1());
System.out.println(students.get(0).getClassInfo().getClassName());
System.out.println(students.get(0).getStuSex()); /*sqlSession.commit();*/
/* sqlSession.rollback();*/
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void testListLikeStu(){
SqlSession sqlSession=MybatisHelper.getSqlSessionFactory().openSession();
try {
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); List<Student> students=studentMapper.findStuLike("张%","1",1);
for (Student s:students){
System.out.println(s.getStuName1());
}
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}@Test
public void testListMapStu(){
SqlSession sqlSession=MybatisHelper.getSqlSessionFactory().openSession();
try {
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
Map<String,Object> map=new HashMap<String, Object>();
map.put("stuName","张%");
map.put("stuSex","1");
map.put("stuId",1);
List<Student> students=studentMapper.findStuMap(map);
for (Student s:students){
System.out.println(s.getStuName1());
}
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void testListLikeStu2(){
SqlSession sqlSession=MybatisHelper.getSqlSessionFactory().openSession();
try {
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); List<Student> students=studentMapper.findStuLike2("张%","1",1);
for (Student s:students){
System.out.println(s.getStuName1());
}
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
}
mybatis所需pom文件内容以及配置文件的更多相关文章
- Maven的pom文件内容详细理解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- sqpark编译时maven的pom文件内容(org.apachetype.maven.apachetypes:maven.archetypequickstat)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 第一个Maven工程的目录结构和文件内容及联网问题
[第一个Maven工程] ①目录结构 Hello |---src |---|---main |---|---|---java |---|---|---resources |---|---test |- ...
- springMVC+Mybatis的maven-web项目的pom.xml文件内容
pom.xml文件内容 <!-- 第一行是XML头,指定了该xml文档的版本和编码方式 --> <project xmlns="http://maven.apache.or ...
- springboot配置文件读取pom文件信息
解决的问题 springboot(当然别的也可以)多环境切换需要该配置文件,打包时不够方便. 解决: 配置文件能读取pom文件中的配置,根据命令选择不同配置注入springboot的配置文件中 pom ...
- mybatis逆向工程的注意事项:mapper文件内容不是覆盖而是追加
XXXMapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件内容不被覆盖而是进行内容追加,结果导致mybatis解析失败. 解决方法:删除原来已经生成的mapper xml文件 ...
- Maven 项目中的 pom.xml 文件内容说明
下面是一个比较全面的 POM 文件的结构,当然常用的并不需要这么多配置,视自己的项目需求而定. <project xmlns="http://maven.apache.org/POM/ ...
- MyEclipse导入Maven项目pom文件第一行报错,运行Tomcat报Log4j错误--解决方法
问题描述: 前一段时间电脑第一次导入Maven项目,又是pom文件错,改好后又是运行Tomcat报Log4j错误,一直倒腾了近一个月程序才成功跑起来,太不容易. 也上网查了很长时间,没一个方法能解决我 ...
- Maven学习详解(13)——Maven常用命令大全与pom文件讲解
一.Maven常用命令 1.1.Maven 参数 -D 传入属性参数 -P 使用pom中指定的配置 -e 显示maven运行出错的信息 -o 离线执行命令,即不去远程仓库更新包 -X 显示ma ...
随机推荐
- Change R source code
If you'd like to simply test out the effect of that change in an interactive R session, you can do s ...
- Tkinter Menu(菜单)
Tkinter Menu: 这个小工具的目标是,让我们来创建我们的应用程序,可以通过使用各种菜单.核心功能,提供的方式来创建三个菜单类型:弹出式,顶层,和下拉 这个小工具的目标是,让我们来创建 ...
- preprocess
1,宏定义,有参宏,无参宏,宏定义实现的是定义一个符号常量; 条件编译3种方式,文件包含含义; 不带参数的宏定义;既用一个指定的的标识符来代替一个字符串; #define RUIY 10000000 ...
- HTTP之URL
URI 和 URL 与 URI(统一资源标识符)相比,我们更熟悉 URL(Uniform Resource Locator,统一资源定位符).URL 正是使用 Web 浏览器等访问 Web 页面时需要 ...
- Gearmand 任务分发系统
简介: Gearmand 是一个用来把工作委派给其它机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来调用其它语言的函数的系统. 简单来讲,就是客户端程序把请求提 ...
- RimLight(轮廓光) - Shader
[RimLight(轮廓光) - Shader] RimLight指的是物体的轮廓光.效果如下: 轮廓光的强度通过 1.0 - dot(normal, eye_vector)来计算.使用这个公式,则指 ...
- HP发送HTTP POST请求 返回结果
HP发送HTTP POST请求 返回结果 <?php $srv_ip = '192.168.10.188';//你的目标服务地址或频道.$srv_port = 80;$url = '/demo/ ...
- leetcode array解题思路
Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...
- ProxyPattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- Linux cloc
一.简介 cloc是一个基于perl的.十分好用的代码统计工具,它所支持的语言还算十分丰富.不过,还是有很多用的较少的语言是不支持的. 二.安装配置 1)官网安装教程 http://cloc.so ...