1. 程式人生 > >Mybatis配置,包括註解和XML配置

Mybatis配置,包括註解和XML配置

1.專案結構


2. 主配置檔案 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="config/jdbc.properties" />
	<!--延遲載入配置setting-->
	<settings>
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	<typeAliases>
		<!-- <typeAlias type="com.zhq.entity" alias="Grade" /> -->
		<package name="com.zhq.entity"/>
	</typeAliases>
	<!-- 對事務的管理和連線池的配置 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<!-- mapping 檔案路徑配置 -->
	<mappers>
		<!-- <mapper resource="com/etc/mappers/UserMapper.xml" /> -->
		<package name="com.zhq.dao"/>
	</mappers>
</configuration>
3. 資料庫配置檔案 jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/myschool?characterEncoding\=utf-8
username=root
password=root
4. 日誌配置檔案 log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.zhq.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
5. 介面配置檔案(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.zhq.dao.GradeDao">
    <!--配置 實體類屬性和資料列的關聯,如果不配置默認同名傳遞,匹配不到的則為空-->
	<resultMap type="Grade" id="g">
		<id column="gradeId" property="gradeId"/>
		<result column="gradeName" property="gradeName"/>
		<!--一對多 -->
		<collection property="students" column="gradeId" select="com.zhq.dao.StudentDao.findByGradeId"></collection>
	</resultMap>

	<select id="find"  resultType="Grade">
		select * from grade
	</select>
	<select id="findById" resultMap="g" parameterType="int">
		select * from grade where  gradeId=#{Id}
	</select>
	<insert id="add" parameterType="Grade" useGeneratedKeys="true" keyProperty="gradeId" >
		insert into grade value(null,#{gradeName})
	</insert>
	<update id="update" parameterType="Grade">
		update grade set gradeName=#{gradeName} where gradeId=#{gradeId} 
	</update>
	<delete id="delete" parameterType="int">
		delete from grade where gradeId=#{id}
	</delete>
</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.zhq.dao.StudentDao">
    <!--二級快取配置cache-->
	<!-- <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"></cache> -->
	<resultMap type="Student" id="studentList">
		<id property="studentNo" column="studentNo"/>
		<result property="name" column="name"/>
		<result property="sex" column="sex"/>
		<result property="age" column="age"/>
		<result property="phone" column="phone"/>
		<result property="address" column="address"/>
		<result property="email" column="email"/>
		<result property="birthday" column="birthday"/>
		<!--多對一  -->
		<association property="grade" column="gradeId" select="com.zhq.dao.GradeDao.findById"></association>
	</resultMap>
	
	<select id="find"  resultMap="studentList">
		select * from student
	</select>
	<select id="find_1"  resultMap="studentList" parameterType="Map">
		select * from student limit #{offset},#{limit}
	</select>
	<select id="findByGradeId"  resultType="Student" parameterType="int">
		select * from student where gradeId=#{gradeId}
	</select>
	<select id="findStudentBySex"  resultType="Student" parameterType="Map" statementType="CALLABLE">
		{call p_findStudentBySex(#{sex})}
	</select>
	<select id="getStudentCount" parameterType="Map" statementType="CALLABLE">
		{call p_getStudentCount(#{count,mode=OUT,jdbcType=INTEGER})}
	</select>
	<select id="search" parameterType="Map" resultMap="studentList">
		select * from student
		<where>
			<if test="name != null">
			    name like #{name}
			</if>
			<if test="sex != null">
			    and sex = #{sex}
			</if>
			<if test="age != 0">
			    and age = #{age}
			</if>
		</where>
	</select>
	<select id="searchStudentByManyName" resultMap="studentList">
		select * from student where name in
		<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>
</mapper> 
6. 介面配置(註解方式)
package com.zhq.dao;

import java.util.List;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.zhq.entity.Admin;

public interface AdminDao {
	@Select("select * from admin")
	@Results({
		@Result(id=true,column="id",property="id"),
		@Result(column="loginId",property="loginId"),
		@Result(column="loginPwd",property="loginPwd"),
		@Result(column="isAdmin",property="isAdmin")
	})
	public List<Admin> findAllAdmin();

}
package com.zhq.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhq.entity.Subject;

public interface SubjectDao {
	//查詢所有課程
	@Select("select * from subject")
	@Results({
		@Result(id=true,column="subjectNo",property="subjectNo"),
		@Result(column="subjectName",property="subjectName"),
		@Result(column="classHour",property="classHour"),
		@Result(column="gradeId",property="grade",[email protected](select="com.zhq.dao.GradeDao.findById"))
	})
	public List<Subject> findAllSubjects();
	//新增新課程
	@Insert("insert into subject value(null,#{subjectName},#{classHour},#{grade.gradeId})")
	public int add(Subject subject);
	//通過ID修改課程
	@Update("update subject set subjectName=#{subjectName},classHour=#{classHour},gradeId=#{grade.gradeId} where subjectNo=#{subjectNo}")
	public int update(Subject subject);
	//刪除課程資訊
	@Delete("delete from subject where subjectNo=#{id}")
	public int delete(int id);

}
7. 實體類
package com.zhq.entity;

public class Admin {
	private int id;
	private String loginId;
	private String loginPwd;
	private String isAdmin;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getLoginId() {
		return loginId;
	}
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}
	public String getLoginPwd() {
		return loginPwd;
	}
	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	public String getIsAdmin() {
		return isAdmin;
	}
	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}
	@Override
	public String toString() {
		return "Admin [id=" + id + ", loginId=" + loginId + ", loginPwd=" + loginPwd + ", isAdmin=" + isAdmin + "]";
	}
	

}
package com.zhq.entity;

import java.util.List;

public class Grade {
 private int gradeId;
 private String gradeName;
 private List<Student> students=null;
public int getGradeId() {
	return gradeId;
}
public void setGradeId(int gradeId) {
	this.gradeId = gradeId;
}
public String getGradeName() {
	return gradeName;
}
public void setGradeName(String gradeName) {
	this.gradeName = gradeName;
}

public List<Student> getStudents() {
	return students;
}
public void setStudents(List<Student> students) {
	this.students = students;
}
@Override
public String toString() {
	return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]";
}
 
}
package com.zhq.entity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Student {
	private int studentNo;
	private String name;
	private String sex;
	private int age;
	private String phone;
	private String address;
	private Date birthday;
	private String email;
	private Grade grade;
	public int getStudentNo() {
		return studentNo;
	}
	public void setStudentNo(int studentNo) {
		this.studentNo = studentNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Override
	public String toString() {
		return "Student [studentNo=" + studentNo + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone="
				+ phone + ", address=" + address + ", birthday=" +  sdf.format(birthday) + ", email=" + email
				+ "]";
	}
	
}
package com.zhq.entity;

public class Subject {
	private int subjectNo;
	private String subjectName;
	private int classHour;
	private Grade grade;
	public int getSubjectNo() {
		return subjectNo;
	}
	public void setSubjectNo(int subjectNo) {
		this.subjectNo = subjectNo;
	}
	public String getSubjectName() {
		return subjectName;
	}
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	public int getClassHour() {
		return classHour;
	}
	public void setClassHour(int classHour) {
		this.classHour = classHour;
	}
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	@Override
	public String toString() {
		return "Subject [subjectNo=" + subjectNo + ", subjectName=" + subjectName + ", classHour=" + classHour
				+ "]";
	}
	
}
8. Dao
package com.zhq.dao;

import java.util.List;

import com.zhq.entity.Grade;

public interface GradeDao {
	 //查詢所有年級資訊
	 public List<Grade> find();
	 //新增新年級
	 public int add(Grade grade);
	 //修改年級資訊
	 public int update(Grade grade);
	 //刪除年級資訊
	 public int delete(int id);
	 //通過年級編號查詢年級資訊
	 public Grade findById(int id);
}
package com.zhq.dao;

import java.util.List;
import java.util.Map;

import com.zhq.entity.Student;

public interface StudentDao {
	//查詢所有學生資訊分頁方式一
	 public List<Student> find();
	//查詢所有學生資訊分頁方式二
	 public List<Student> find_1(Map<String,Integer> map);
	//通過年級查學生
	 public List<Student> findByGradeId(int id);
	//通過性別查學生
	 public List<Student> findStudentBySex(Map<String,String> map);
	//查詢全校學生總數
	 public void getStudentCount(Map<String,Integer> map);
    //多條件查詢
	 public List<Student> search(Map<Object,Object> map);
	//模糊查詢不同名字的學生
	 public List<Student> searchStudentByManyName(List<String> namelist);
}
9. Service
package com.zhq.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.zhq.dao.StudentDao;
import com.zhq.entity.Student;
import com.zhq.utils.SQLSessionFactory;

public class StudentService {
	//分頁方法一:查詢學生offset(開始下標),limit(查詢行數)
	public List<Student> find(int offset,int limit){
		//讀取主配置檔案,把主配置檔案進行XML解析,解析成輸入流
		SqlSession sqlSession = SQLSessionFactory.getSession();
		/*List<Grade> list =  sqlSession.selectList("find");*/
		/*StudentDao dao=sqlSession.getMapper(StudentDao.class);*/
		RowBounds rb=new RowBounds(offset, limit);
		List<Student> list = sqlSession.selectList("com.zhq.dao.StudentDao.find", null, rb) ;
		sqlSession.close();  //關閉連線物件
		return list;		
		}
	//分頁方法二:查詢學生offset(開始下標),limit(查詢行數)
	public List<Student> find_1(int offset,int limit){
		//讀取主配置檔案,把主配置檔案進行XML解析,解析成輸入流
		SqlSession sqlSession = SQLSessionFactory.getSession();
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("offset", offset);
		map.put("limit", limit);
		StudentDao dao=sqlSession.getMapper(StudentDao.class);
		List<Student> list=dao.find_1(map);
		
		sqlSession.close();  //關閉連線物件
		return list;		
		}
	//通過性別查詢學生
	public List<Student> findStudentBySex(String sex){
		//讀取主配置檔案,把主配置檔案進行XML解析,解析成輸入流
		SqlSession sqlSession = SQLSessionFactory.getSession();
		Map<String,String> map=new HashMap<String,String>();
		map.put("sex", sex);
		StudentDao dao=sqlSession.getMapper(StudentDao.class);
		List<Student> list=dao.findStudentBySex(map);
		
		sqlSession.close();  //關閉連線物件
		return list;		
	}
	//獲取學生總人數
	 public int getStudentCount() {
		 SqlSession sqlSession = SQLSessionFactory.getSession();
			Map<String,Integer> map=new HashMap<String,Integer>();
			map.put("count", 0);
			StudentDao dao=sqlSession.getMapper(StudentDao.class);
			dao.getStudentCount(map);
			sqlSession.close();  //關閉連線物件
			return map.get("count");	
	 }
	//多條件動態查詢
	 public List<Student> search(String name,String sex,int age){
		//讀取主配置檔案,把主配置檔案進行XML解析,解析成輸入流
			SqlSession sqlSession = SQLSessionFactory.getSession();
			Map<Object,Object> map=new HashMap<Object,Object>();
			map.put("name", name);
			map.put("sex", sex);
			map.put("age", age);
			StudentDao dao=sqlSession.getMapper(StudentDao.class);
			List<Student> list=dao.search(map);
			sqlSession.close();  //關閉連線物件
			return list;
	 }
	//模糊查詢不同名字的學生
	public List<Student> searchStudentByManyName(List<String> namelist){
		SqlSession sqlSession = SQLSessionFactory.getSession();
		StudentDao dao=sqlSession.getMapper(StudentDao.class);
		List<Student> list=dao.searchStudentByManyName(namelist);
		return list;
	}
}
package com.zhq.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zhq.dao.GradeDao;
import com.zhq.entity.Grade;
import com.zhq.utils.SQLSessionFactory;

public class GradeService {
	public List<Grade> find(){
	//讀取主配置檔案,把主配置檔案進行XML解析,解析成輸入流
	SqlSession sqlSession = SQLSessionFactory.getSession();
	/*List<Grade> list =  sqlSession.selectList("find");*/
	GradeDao dao=sqlSession.getMapper(GradeDao.class);
	List<Grade> list = dao.find() ;
	sqlSession.close();  //關閉連線物件
	return list;
		
	}
	public int add(Grade grade) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		GradeDao dao=sqlSession.getMapper(GradeDao.class);
		int result=dao.add(grade);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
	public int update(Grade grade) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		GradeDao dao=sqlSession.getMapper(GradeDao.class);
		int result=dao.update(grade);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
	public int delete(int id) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		GradeDao dao=sqlSession.getMapper(GradeDao.class);
		int result=dao.delete(id);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
	public Grade findById(int id) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		GradeDao dao=sqlSession.getMapper(GradeDao.class);
		Grade result=dao.findById(id);
		sqlSession.close();
		return result;
	}
}
package com.zhq.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zhq.dao.AdminDao;
import com.zhq.entity.Admin;
import com.zhq.utils.SQLSessionFactory;

public class AdminService {
	public List<Admin> findAllAdmin(){
		SqlSession sqlSession = SQLSessionFactory.getSession();
		/*List<Grade> list =  sqlSession.selectList("find");*/
		AdminDao dao=sqlSession.getMapper(AdminDao.class);
		List<Admin> list = dao.findAllAdmin();
		sqlSession.close();  //關閉連線物件
		return list;	
	}
}
package com.zhq.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zhq.dao.SubjectDao;
import com.zhq.entity.Subject;
import com.zhq.utils.SQLSessionFactory;

public class SubjectService {
	//查詢所有課程
	public List<Subject> findAllSubjects(){
		SqlSession sqlSession = SQLSessionFactory.getSession();
		SubjectDao dao=sqlSession.getMapper(SubjectDao.class);
		List<Subject> list = dao.findAllSubjects();
		sqlSession.close();  //關閉連線物件
		return list;
	}
	//新增新課程
	public int add(Subject subject) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		SubjectDao dao=sqlSession.getMapper(SubjectDao.class);
		int result=dao.add(subject);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
	//修改課程
	public int update(Subject subject) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		SubjectDao dao=sqlSession.getMapper(SubjectDao.class);
		int result=dao.update(subject);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
	//刪除某個課程資訊
	public int delete(int id) {
		SqlSession sqlSession=SQLSessionFactory.getSession();
		SubjectDao dao=sqlSession.getMapper(SubjectDao.class);
		int result=dao.delete(id);
		sqlSession.commit();
		sqlSession.close();
		return result;
	}
}
10. Utils
package com.zhq.utils;

import java.io.IOException;
import java.io.Reader;

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 SQLSessionFactory {
	static Reader reader = null;
	static SqlSessionFactory sqlSessionFactory=null;
	private  SQLSessionFactory() {}
	//這是一個靜態語句塊,用於讀取主配置檔案,因為主配置檔案只需要讀取一次。
	static {
		try {
			//形成輸入流
			reader = Resources.getResourceAsReader("config/mybatis-config.xml");
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	//SqlSession工廠,只需要一個,所以做成單例模式 
	public static SqlSessionFactory  getSqlSessionFactory(){
		if(sqlSessionFactory == null){
			synchronized (SQLSessionFactory.class) {
				if(sqlSessionFactory == null){
					sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
				}
			}	
		}
		return sqlSessionFactory;
	}
	public static SqlSession getSession(){
		//返回一個數據操作連線物件
		return getSqlSessionFactory().openSession();
	}
}
11. Test
package com.zhq.controller;

import java.util.List;

import com.zhq.entity.Admin;
import com.zhq.service.AdminService;

public class AdminTest {

	public static void main(String[] args) {
		AdminService ser=new AdminService();
		List<Admin>list=ser.findAllAdmin();
		for(Admin admin:list) {
			System.out.println(admin);
		}

	}

}
package com.zhq.controller;

import java.util.List;

import javax.swing.text.AbstractDocument.Content;

import com.zhq.entity.Grade;
import com.zhq.entity.Student;
import com.zhq.service.GradeService;

public class GradeTest {

	public static void main(String[] args) {
		GradeService ser=new GradeService();
		//查詢所有年級
		/*List<Grade>list=ser.find();
		for(Grade grade:list) {
			System.out.println(grade);
		}*/
		//通過ID查年級學生
		Grade g=ser.findById(3);
		if(g!=null) {
			System.out.println(g);
			if(g.getStudents()!=null) {
				for(Student student:g.getStudents()) {
					System.out.println(student);
				}
			}
		}
		
		/*//新增新年級
		Grade grade=new Grade();
		grade.setGradeName("9年級");
		if(ser.add(grade)>0) {
			System.out.println("新增成功!");
			System.out.println("新增新的ID:"+grade.getGradeId());
		}else {
			System.out.println("新增失敗!");
		}*/
		//修改年級
		/*GradeService ser=new GradeService();
		Grade grade=new Grade();
		grade.setGradeName("十年級");
		grade.setGradeId(19);
		if(ser.update(grade)>0) {
			System.out.println("修改成功!");
		}else {
			System.out.println("修改失敗!");
		}*/
		//刪除年級
		/*GradeService ser=new GradeService();
		if(ser.delete(19)>0) {
			System.out.println("刪除成功!");
		}else {
			System.out.println("刪除失敗!");
		}*/
	}

}
package com.zhq.controller;

import java.util.ArrayList;
import java.util.List;

import com.zhq.entity.Student;
import com.zhq.service.StudentService;

public class StudentTest {
	public static void main(String[] args) {
		StudentService ser= new StudentService();
		//分頁查詢學生
		/*System.out.println("分頁方式一:");//適用資料量不大的情況
		for(Student student:ser.find(5,5)) {
			System.out.println(student);
		}
		System.out.println("分頁方式二:");//適用資料量大的情況
		for(Student student:ser.find_1(5,5)) {
			System.out.println(student);
		}*/
		//按性別查詢學生
		/*for(Student student:ser.findStudentBySex("女")) {
			System.out.println(student);
		}*/
		/*System.out.println("全校總人數:"+ser.getStudentCount());*/
		//動態SQL多條件查詢
		/*for(Student student:ser.search("%李%","男",0)) {
			System.out.println(student);
		}*/
		//動態SQL模糊查詢不同名字的學生
		List<String> namelist=new ArrayList<String>();
		namelist.add("李小軍");
		namelist.add("李相赫");
		namelist.add("劉巧妹");
		namelist.add("李豔紅");
		for(Student student:ser.searchStudentByManyName(namelist)) {
			System.out.println(student);
		}
	}
}
package com.zhq.controller;

import com.zhq.entity.Grade;
import com.zhq.entity.Subject;
import com.zhq.service.SubjectService;

public class SubjectTest {

	public static void main(String[] args) {
		SubjectService ser=new SubjectService();
		//新增新課程
		Subject sub1=new Subject();
		Grade grade=new Grade();
		grade.setGradeId(4);
		sub1.setSubjectName("MySpring");
		sub1.setClassHour(80);
		sub1.setGrade(grade);
		/*if(ser.add(sub1)>0) {
			System.out.println("新增成功!");
		}else {
			System.out.println("新增失敗!");
		}*/
		//修改課程
		/*sub1.setSubjectNo(14);
		sub1.setClassHour(100);
		if(ser.update(sub1)>0) {
			System.out.println("修改成功!");
		}else {
			System.out.println("修改失敗!");
		}*/
		/*if(ser.delete(15)>0) {
			System.out.println("刪除成功!");
		}else {
			System.out.println("刪除失敗!");
		}*/
		//查詢所有課程
		for(Subject sub:ser.findAllSubjects()) {
			System.out.println(sub.getGrade().getGradeName()+sub);
		}

	}

}





相關推薦

Mybatis配置包括註解XML配置

1.專案結構 2. 主配置檔案 mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org

springboot+mybatis+druid實現多資料來源配置支援註解xml兩種sql書寫方式

https://github.com/cheegoday/springboot-demo-djg 要點: 一、依次建立以下幾個Bean 資料來源:DataSource session工廠:SqlSessionFactory 執行緒安全session:Sql

spring自帶的定時任務功能基於註解xml配置

1、spring的配置檔案 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml

基於spring註解xml配置的webService

1.需要的jar包 commons-logging-1.1.1.jar cxf-2.4.3.jar neethi-3.0.1.jar spring-aop-3.0.5.RELEASE.jar spring-asm-3.0.5.RELEASE.jar spring-beans

SpringBoot整合MyBatis(iBatis)基於註解XML兩種方式

工具 IDEA Maven 專案建立 1. 通過IDEA建立SpringBoot專案 2. 結構目錄和JAVA版本選擇 3. 新增MySQL和MyBatis支援 4. 新增Lombok外掛,簡化GET、SET方法 5. WEB支援和啟動類 6. 專

Spring AOP 之AspectJ註解XML配置兩種實現(Maven構建)

xml配置 1.介面和實現類 public interface UserManager { public String findUserById(int userId); } @Service public class UserManage

MyBatis之resultMap的作用xml配置折騰

簡單的搭建。我這裡是使用了xml加interface結合的方法進行操作。現有Employee類。如果是簡單的select,可以看看下面簡單的例子。Employee.javapublic class Employee {//省略get set 方法 private int i

mybatis使用註解替代xml配置動態生成Sql

key list 數據 很難 pro ring 參考 rec 不知道 mybatis使用註解替代xml配置時,遇到判斷條件是否為null或者為空時,@Select很難搞定,不知道怎麽辦? mybatis3中增加了使用註解來配置Mapper的新特性,使用 SelectPro

Spring Boot通過Mybatis使用mapper介面xml配置sql連線資料庫

由於最早使用的是註解的方式來連線資料庫,所以比較簡單,後來發行做後臺的同事用的一般都是xml的形式,所以也跟著學習了下。發行mapper介面和xml配置sql,比較複雜,尤其是對我這種新手,研究了好久才配置成功。為了防止忘記,故寫下來,也給準備學習的人當參考

springboot+mybatis多資料來源配置AOP註解動態切換資料來源

轉載至:https://blog.csdn.net/xiaosheng_papa/article/details/80218006 親測有效。 注:有些系統中已經配置了單資料來源,現在要轉成多資料來源,可能需要額外的配置。拿我自己當前專案來說: 專案在啟動類中配置了單資料來源:

mybatis系列之入門XML配置

一、為什麼使用mybatis 1.1 JDBC處理過程 JDBC功能簡單,一般需要上門五個過程即可; sql語句編寫在java程式碼裡面;硬編碼高耦合的方式; 維護不易且實際開發需求中sql是有變化,頻繁修改的情況多見 ; JDBC這種方式一般不推薦。其他框

SpringBoot整合MyBatis(註解版+XML配置版)

一.公共部分 1.建立SpringBoot專案,加入依賴 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs

使用註解自動解釋orm從而實現無xml配置的jdbc泛型介面dao對資料庫單表增刪查改

一、獲取資料庫連線和關閉資源 package cn.itdoer.base.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepa

SSM中進行註解XML配置式事務管理

場景 前面實現SSM簡單整合以及CRUD參照: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85161018 SSM中配置事務管理所需jar包: https://download.csdn.net/downloa

Spring4深入理解----事務(宣告式事務xml配置事務事務傳播屬性事務其他屬性(隔離級別&回滾&只讀&過期))

  •事務管理是企業級應用程式開發中必不可少的技術,  用來確保資料的完整性和一致性.   •事務就是一系列的動作,它們被當做一個單獨的工作單元.這些動作要麼全部完成,要麼全部不起作用   •事務的四個關鍵屬性(ACID)     –原子性(atomicity):事務是一

SpringBoot + MyBatis註解xml

起因 最近寫程式碼,使用MyBatis連線資料庫,卻總是掃描不要mapper.xml 檔案。臨時用註解的方式解決了問題。後面又轉過頭來,用xml檔案實現了一次。 註解方式 例如,我在dao中,實現一個查詢賬號密碼的語句: @Mapper public interface Lo

自動裝配、JavaConfig、XML 三種方案之間怎麼匯入混合配置

在 Spring 中,這些配置方案都不是互斥的。完全可以將 JavaConfig 的元件掃描和自動裝配/或 XML 配置混合在一起。 Q:如何在 JavaConfig 中引用 XML 配置? Q:怎麼將兩個 JavaConfig 類組合在一起? A:①

Spring多利模式 註解@Scope("prototype")xml配置scope="prototype"

這裡牽扯到單利模式的執行緒安全的設計。 我們知道sping ioc注入的bean;一般都是無狀態的【dao,service等,這種不會牽涉到值或者狀態改變的情況】,也就是在多執行緒下可以基本保證執行緒

Spring ApplicationContext.xml 配置檔案常用註解詳解

ApplicationContext.xml <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans"

git config配置工作區版本庫聯系。

linu htm 相關 lfs global intro git bash .text desktop 關於git和github的介紹,我這邊不多說。 使用在windows下使用git,需要配置環境變量,也可以使用git自帶的終端工具。,打開git bash [e