Some details of the problems occurring in MyBatis simple implementation (advanced issues) a type converter; 2. set individual alias; 3 configuration information in the code data is made easier. 4.statement simplified

Processor type (type converter )
1.MyBatis comes with some common types of processors

	int-number

2. Custom Types processor MyBatis

java-数据库(jdbc类型)

for instance:

Note: The table and the entity class name can not be the same, but the property must be the same name.

Entity class Student: stuSex

true:男。false:女

Table Student: stuSex
1: Male. 0: female

Custom type converter (boolean-number) in step
a converter to create: a need to implement the interface

但是发现接口实现比较麻烦,所以继承其类,等于间接继承该类。

. B directly on the code: code inside specific description are explained.

//BaseTypeHandler<java类型>
public class BoolrsnAndIntConverter extends BaseTypeHandler<Boolean>{

@Override

/**
 * java-DB
 * ps:PreparedStatement对象
 * i:PreparedStatement对象操作参数得位置
 * parameter:java值
 * jdbcType:jdbc操作得数据库类型
 * */
public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType)
		throws SQLException {
	// TODO 自动生成的方法存根
	if(parameter) {
		//如果parameter的值是true,那么数据库中性别框子中将会存储1
		ps.setInt(i, 1);
	}
	else {
		//如果parameter的值是false,那么数据库中性别框子中将会存储0
		ps.setInt(i, 0);
	}
	
}

/**
 * db-java
 * */
@Override
public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
	// TODO 自动生成的方法存根
	int sexNum=rs.getInt(columnName);
	if(sexNum==1) {
		return true;
	}else {
		return false;
	}
}

@Override
public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	// TODO 自动生成的方法存根
	int sexNum=rs.getInt(columnIndex);
	if(sexNum==1) {
		return true;
	}else {
		return false;
	}
}

@Override
public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	// TODO 自动生成的方法存根
	int sexNum=cs.getInt(columnIndex);
	if(sexNum==1) {
		return true;
	}else {
		return false;
	}
}

Set a single alias

	 <!-- 后续通过namespace:id -->
	 <!-- resultType:查询返回结果值得类型,返回类型 -->
	 <select id="queryStudenyByStuno" resultType="student" parameterType="int">
	 	select * from student where stuNo =${value}
	 </select>

parameterType = "int" there is an incoming parameter type.
resultType = "student" there is a return type.

Then you need to configure in config.xml

	 <!-- 设置单个/多个别名 -->
	<typeAliases>
   		 <typeAlias type="org.awen.entity.Student" alias="student"/>
	</typeAliases>

Configuration data information will become more concise code can create a point in the src db.properties

Wherein the configuration database information to

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName =Employees
username=sa
password=1228

Then you have to file config.xml and then modify it.

					 <dataSource type="POOLED">
						 <!-- 配置数据信息 -->
						 <property name="driver" value="${driver}"/>
						 <property name="url" value="${url}"/>
						 <property name="username" value="${username}"/>
						 <property name="password" value="${password}"/>
					 </dataSource>

4.statement simplistic
at first statement is written like this, but when you order more time to write some code like this are not very standardized, and it was not particularly simple.

String statement="org.awen.entity.studentMapper."+"queryStudenyByStuno";

This time, direct a new package to build a interface in the package. Inside write some methods, here to note the names of these methods and the type of return also pass in parameters, and you are in mapper.xml the id = "queryStudenyByStuno" resultType = "student " parameterType = "int "
it is one to one.

For example, a similar function like this in xml:

 <select id="queryStudenyByStuno" resultType="student" parameterType="int">
 	select * from student where stuNo =${value}
 </select>

Then you have to write like this in the interface:

Student queryStudenyByStuno(int stuNo);

Here or draw a picture to explain:This is almost the

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/91356611