Mybatisベース-01-最初のMybatis
6567 ワード
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
src/main/resource log 4 j.properties#log4j.rootLogger=debug,console
log4j.logger.monkey1024=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
src/main/java mybatis.xml
StudentMapper.xml
INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
Student.javapackage com.monkey1024.bean;
public class Student {
private int id;
private String name;
private int age;
private double score;
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;
}
}
Student Dao.javapackage com.monkey1024.dao;
import com.monkey1024.bean.Student;
public interface StudentDao {
void insertStudent(Student student);
}
StuentDao Impl.javapackage com.monkey1024.dao.impl;
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;
import com.monkey1024.bean.Student;
import com.monkey1024.dao.StudentDao;
public class StudentDaoImpl implements StudentDao {
private SqlSession sqlSession;
@Override
public void insertStudent(Student student) {
try {
//
InputStream input = Resources.getResourceAsStream("mybatis.xml");
// SqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(input);
// SqlSession
sqlSession = sessionFactory.openSession();
//
sqlSession.insert("insertStudent", student);
// SqlSession
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
Student Test 01.javapackage com.monkey1024.test;
import org.junit.Test;
import com.monkey1024.bean.Student;
import com.monkey1024.dao.StudentDao;
import com.monkey1024.dao.impl.StudentDaoImpl;
public class StudentTest01 {
@Test
public void insertStudent(){
StudentDao studentDao = new StudentDaoImpl();
Student student = new Student("jak",52,98.5);
studentDao.insertStudent(student);
}
}