.net转java 之 MyBatis入门教程


1、环境:



2、配置:

2.1)创建Config文件夹,然后创建SqlMapConfig.XML







	
	

	
		
		

		
		
	

	
	
		
			
			
			
			
				
				
				
				
			
		
	

	
	
		
		

		
		
	


2.2)创建jdbc.properties文件,配置数据库参数

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Article?characterEncoding=utf8&useSSL=true
jdbc.name=rootjdbc.password=123456

注:

当JDBC版本与MySQL版本不兼容,MySQL的版本更高一些 时 会报下面的错误: 

 
  

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决方法是:在连接语句后加上“useSSL=‘true’” ,就可以连接到数据库了

3、创建Mapper,这个的UserMapper.xml和UserMapper.java 要放到一个文件夹中,并且两个文件名要一致,这个才能自动扫描包<package name="wft.map" />,不用一个个的配置

3.1)使用自动扫描包要遵受以下4点:(更正一下:namespace必须和接口的包命一致,java称为包不是命名空间)

粘出接口方便园友对比。

4、创建测试

这里用是的JUit4,不要把Sqlsession设置为全局变量,因为:

更新操作根据ID更新,先找出ID对应的,再进行更新,因为全局变量,所以testQueryUserById和testUpdateUser是共用一个sqlSession,但是在执行第一个方法时,由于习惯自己把事务关闭了,所以无法进行Update

解决办法:

1.每个方法对应一个SqlSession对象,依旧是在每个方法的开头,写

Sqlsession sqlSession = MyBatisUtil.getSqlSession;

2.记住select方法不需要提交事务,也不需要关闭对象

package wft.test;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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 org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import wft.map.UserMapper;
import wft.model.UserModel;

public class UserMybatis {

	SqlSessionFactory sqlSessionFactory = null;
	UserMapper userMapper = null;

	@BeforeClass
	public static void globalInit() throws Exception {

	}

	@Before
	public void setUp() throws Exception {
		String resource = "SqlMapConfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	}

	@Test
	public void testQueryUserById() throws Exception {

		SqlSession sqlSession = sqlSessionFactory.openSession();
		userMapper = sqlSession.getMapper(UserMapper.class);

		UserModel userModel = userMapper.queryUserById(30);

		System.out.println(userModel);
	}

	@Test
	public void testUpdateUser() throws Exception {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		userMapper = sqlSession.getMapper(UserMapper.class);

		UserModel userModel = userMapper.queryUserById(25);
		userModel.setUserName("小车");
		userModel.setAddress("北京朝阳区");

		userMapper.updateUser(userModel);
		sqlSession.commit();
		sqlSession.close();
	}





















你可能感兴趣的:(java)