pom.xml
4.0.0
com.monkey1024
01mybatis
0.0.1-SNAPSHOT
war
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
5.1.46
log4j
log4j
1.2.17
commons-io
commons-io
2.6
commons-fileupload
commons-fileupload
1.3.3
org.hibernate
hibernate-validator
6.0.9.Final
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-webmvc
5.0.4.RELEASE
01mybatis
src/main/java
**/*.xml
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/learnmybatis?useSSL=false
jdbc.user=root
jdbc.password=666666
log4j.properties
log4j.rootLogger=debug,console
#log4j.logger.com.monkey1024.dao.StudentDao=debug,concole
#\u63A7\u5236\u53F0\u9644\u52A0\u5668
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%-5p][%d{yyyy-MM--dd HH:mm:ss}]%m%n
mybatis.xml
StudentMapper.xml
select id,name,age,score
from t_student
where 1=1
AND name like '%' #{name} '%'
AND age>#{age}
select id,name,age,score
from t_student
AND name like '%' #{name} '%'
AND age>#{age}
select id,name,age,score
from t_student
name like '%' #{name} '%'
age>#{age}
1!=1
select id,name,age,score
from t_student
where id in
#{id}
select id,name,age,score
from t_student
where id in
#{id}
select id,name,age,score
from t_student
where id in
#{stu.id}
select id,name,age,score
from t_student
and name like '%' #{name} '%'
and age < #{age}
and age #{age}
Student.java
package com.monkey1024.bean;
//鎵嬪姩鐨勮缃埆鍚�
//@Alias("stu")
public class Student {
private int id;
private String name;
private int age;
private double score;
private String pwd;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Student(int id, String name, int age, double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
public Student(){
}
public Student(int id, String name, int age, double score, String pwd) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
this.pwd = pwd;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + ", pwd=" + pwd + "]";
}
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public void setName(String name) {
this.name = name;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
}
StudentDao.java
package com.monkey1024.dao;
import java.util.List;
import com.monkey1024.bean.Student;
public interface StudentDao {
List selectIf(Student student);
List selectWhere(Student student);
List selectChoose(Student student);
List selectForeachArray(int[] ids);
List selectForeachList(List list);
List selectForeachListStudent(Listlist);
}
StudentTest01.java
package com.monkey1024.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.monkey1024.bean.Student;
import com.monkey1024.dao.StudentDao;
import com.monkey1024.util.MyBatisUtil;
public class StudentTest01 {
private StudentDao studentDao;
private SqlSession sqlSession;
@Before
public void init(){
sqlSession = MyBatisUtil.getSqlSession();
//鑾峰彇studentDao鐨勫璞�
//mapper鍔ㄦ�佷唬鐞�
studentDao = sqlSession.getMapper(StudentDao.class);
}
/*
* 鏂规硶鎵ц瀹屾垚鍚庯紝闇�瑕佸叧闂璼qlSession
*/
@After
public void closeSession(){
if(sqlSession !=null){
sqlSession.close();
}
}
@Test
public void selectIf(){
Student student = new Student("d",0,0.0);
List students = studentDao.selectIf(student);
students.forEach(s->{
System.out.println(s);
});
}
@Test
public void selectWhere(){
Student student = new Student("s",0,0.0);
List students = studentDao.selectWhere(student);
System.out.println(students);
}
@Test
public void selectChoose(){
Student student = new Student("",50,0.0);
List students = studentDao.selectChoose(student);
students.forEach(s->{
System.out.println(s);
});
}
@Test
public void selectForeachArray(){
int[] ids = {1,3,5,7};
List students = studentDao.selectForeachArray(ids);
students.forEach(s->{
System.out.println(s);
});
}
@Test
public void selectForeachList(){
List ids = new ArrayList();
ids.add(1);
ids.add(3);
List students = studentDao.selectForeachList(ids);
students.forEach(s->{
System.out.println(s);
});
}
@Test
public void selectForeachListStudent(){
List stuList = new ArrayList();
Student s1 = new Student();
Student s2 = new Student();
s1.setId(1);
s2.setId(2);
stuList.add(s1);
stuList.add(s2);
List students = studentDao.selectForeachListStudent(stuList);
students.forEach(s->{
System.out.println(s);
});
}
}
MyBatisUtil.java
package com.monkey1024.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;
/*
* DCL鐨勫崟渚嬫ā寮�
*/
public class MyBatisUtil {
// 鏃犻渶灏嗘瀯閫犳柟娉曠鏈夊寲锛屽洜涓鸿繖閲岄潰鍙淇濊瘉鍒涘缓涓�涓猄qlSessionFactory鐨勫璞�
// private MyBatisUtil()
private static volatile SqlSessionFactory sqlSessionFactory;
public static SqlSession getSqlSession() {
try {
if (sqlSessionFactory == null) {
// 璇诲彇涓婚厤缃枃浠�
InputStream input = Resources.getResourceAsStream("mybatis.xml");
synchronized (MyBatisUtil.class) {
if (sqlSessionFactory == null) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
//mybatis鑷姩鎻愪氦浜嬪姟
// return sqlSessionFactory.openSession(true);
return sqlSessionFactory.openSession();
}
}