您当前的位置: 首页 >  mybatis

星夜孤帆

暂无认证

  • 8浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MyBatis基础02-改写第一个MyBatis程序

星夜孤帆 发布时间:2019-04-23 10:59:14 ,浏览量:8

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
                
            
        
    
 log4j.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
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
mybatis.xml




	
	
	
	
	
	
		
		
		
		
	
	
	
    
        
            
            
                
                
                
                
            
        
    
    
        
        
    
StudentMapper.xml



	
	
		INSERT INTO t_student(name,age,score) VALUES (#{name},#{age},#{score})
		
		
			select @@identity
		
	
	
Student.java
package com.monkey1024.bean;


//手动的设置别名
//@Alias("stu")
public class Student {
	private int id;
	private String name;
	private int age;
	private double score;
	
	
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + 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;
	}
}
StudentDao.java
package com.monkey1024.dao;

import com.monkey1024.bean.Student;

public interface StudentDao {
	void  insertStudent(Student student);
}
StudentDaoimpl.java
package com.monkey1024.dao.impl;

import org.apache.ibatis.session.SqlSession;

import com.monkey1024.bean.Student;
import com.monkey1024.dao.StudentDao;
import com.monkey1024.util.MyBatisUtil;

public class StudentDaoImpl implements StudentDao {

	@Override
	public void insertStudent(Student student) {
		// sqlSession实现了AutoCloseable接口,所以可以自动关闭
		try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
			// 新增操作
			sqlSession.insert("insertStudent", student);
			
			//主键的生成跟事务是否提交没有关系,只要执行了sql语句,那就会分配一个主键
			System.out.println("提交事务之前: "+student);
			// 提交事务
			sqlSession.commit();
			//回滚
//			sqlSession.rollback(); 
		}
	}
}
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 {
	// 无需将构造方法私有化,因为这里面只要保证创建一个SqlSessionFactory的对象
	// 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();

	}
}
StudentTest01.java
package 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("jyd",52,98.5);
		
		//id的默认值是0
		System.out.println(student);
		studentDao.insertStudent(student);
		//可以获取到id的值
		System.out.println(student);
	}
}

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.1108s