/* 建立数据库 */
CREATE DATABASE STUDENT_MANAGER;
USE STUDENT_MANAGER;
/***** 建立student表 *****/
CREATE TABLE STUDENT_TBL
(
STUDENT_ID VARCHAR(255) PRIMARY KEY,
STUDENT_NAME VARCHAR(10) NOT NULL,
STUDENT_SEX VARCHAR(10),
STUDENT_BIRTHDAY DATE,
CLASS_ID VARCHAR(255)
);
/*插入学生数据*/
INSERT INTO STUDENT_TBL (STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
CLASS_ID)
VALUES (123456,
'某某某',
'女',
'1980-08-01',
121546
)
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 3096154202413606831L;
private ClassEntity classEntity;
private Date studentBirthday;
private String studentID;
private String studentName;
private String studentSex;
public ClassEntity getClassEntity() {
return classEntity;
}
public Date getStudentBirthday() {
return studentBirthday;
}
public String getStudentID() {
return studentID;
}
public String getStudentName() {
return studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setClassEntity(ClassEntity classEntity) {
this.classEntity = classEntity;
}
public void setStudentBirthday(Date studentBirthday) {
this.studentBirthday = studentBirthday;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentSex(String studentSex) {
this.studentSex = studentSex;
}
}
public interface StudentMapper {
public StudentEntity getStudent(String studentID);
public StudentEntity getStudentAndClass(String studentID);
public List<StudentEntity> getStudentAll();
public void insertStudent(StudentEntity entity);
public void deleteStudent(StudentEntity entity);
public void updateStudent(StudentEntity entity);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.manager.data.StudentMapper">
<resultMap type="StudentEntity" id="studentResultMap">
<id property="studentID" column="STUDENT_ID"/>
<result property="studentName" column="STUDENT_NAME"/>
<result property="studentSex" column="STUDENT_SEX"/>
<result property="studentBirthday" column="STUDENT_BIRTHDAY"/>
</resultMap>
<!-- 查询学生,根据id -->
<select id="getStudent" parameterType="String" resultType="StudentEntity" resultMap="studentResultMap">
<![CDATA[
SELECT * from STUDENT_TBL ST
WHERE ST.STUDENT_ID = #{studentID}
]]>
</select>
<!-- 查询学生列表 -->
<select id="getStudentAll" resultType="com.manager.data.model.StudentEntity" resultMap="studentResultMap">
<![CDATA[
SELECT * from STUDENT_TBL
]]>
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/>
</typeAliases>
<mappers>
<mapper resource="com/manager/data/maps/StudentMapper.xml" />
</mappers>
</configuration>
<!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:mysql.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!— mapper bean -->
<bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean">
<property name="mapperInterface" value="com.manager.data.StudentMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
@Repository
@Transactional
public interface StudentMapper {
}
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> <property name="basePackage" value="com.liming.manager"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
@Controller
public class TestController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping(value = "index.do")
public void indexPage() {
StudentEntity entity = studentMapper.getStudent("10000013");
System.out.println("name:" + entity.getStudentName());
}
}
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "test-servlet.xml")
public class StudentMapperTest {
@Autowired
private ClassMapper classMapper;
@Autowired
private StudentMapper studentMapper;
@Transactional
public void getStudentTest(){
StudentEntity entity = studentMapper.getStudent("10000013");
System.out.println("" + entity.getStudentID() + entity.getStudentName());
List<StudentEntity> studentList = studentMapper.getStudentAll();
for( StudentEntity entityTemp : studentList){
System.out.println(entityTemp.getStudentName());
}
}
}
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean>
configuration |--- properties |--- settings |--- typeAliases |--- typeHandlers |--- objectFactory |--- plugins |--- environments |--- |--- environment |--- |--- |--- transactionManager |--- |--- |__ dataSource |__ mappers
<!-- 属性替换 --> <properties resource="mysql.properties"> <property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/> <property name="jdbc.url" value="jdbc:mysql://localhost:3306/student_manager"/> <property name="username" value="root"/> <property name="password" value="limingnihao"/> </properties>
|
设置项 |
描述 | 允许值 | 默认值 |
| cacheEnabled | 对在此配置文件下的所有cache 进行全局性开/关设置。 | true | false | true |
| lazyLoadingEnabled | 全局性设置懒加载。如果设为‘false',则所有相关联的都会被初始化加载。 | true | false | true |
| aggressiveLazyLoading | 当设置为‘true'的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。 | true | false | true |
| multipleResultSetsEnabled | 允许和不允许单条语句返回多个数据集(取决于驱动需求) | true | false | true |
| useColumnLabel | 使用列标签代替列名称。不同的驱动器有不同的作法。参考一下驱动器文档,或者用这两个不同的选项进行测试一下。 | true | false | true |
| useGeneratedKeys | 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 | true | false | false |
| autoMappingBehavior | 指定MyBatis 是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,没有嵌套的结果。FULL 将自动映射所有复杂的结果。 | NONE, PARTIAL, FULL | PARTIAL |
| defaultExecutorType | 配置和设定执行器,SIMPLE 执行器执行其它语句。REUSE 执行器可能重复使用prepared statements 语句,BATCH执行器可以重复执行语句和批量更新。 | SIMPLE REUSE BATCH | SIMPLE |
| defaultStatementTimeout | 设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时 | 正整数 | Not Set (null) |
<settings> <setting name="cacheEnabled" value="true" /> <setting name="lazyLoadingEnabled" value="true" /> <setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="false" /> <setting name="enhancementEnabled" value="false" /> <setting name="defaultExecutorType" value="SIMPLE" /> </settings>
<typeAliases> <typeAlias alias="UserEntity" type="com.manager.data.model.UserEntity" /> <typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity" /> <typeAlias alias="ClassEntity" type="com.manager.data.model.ClassEntity" /> </typeAliases>
|
别名 |
映射的类型 |
| _byte | byte |
| _long | long |
| _short | short |
| _int | int |
| _integer | int |
| _double | double |
| _float | float |
| _boolean | boolean |
| string | String |
| byte | Byte |
| long | Long |
| short | Short |
| int | Integer |
| integer | Integer |
| double | Double |
| float | Float |
| boolean | Boolean |
| date | Date |
| decimal | BigDecimal |
| bigdecimal | BigDecimal |
| object | Object |
| map | Map |
| hashmap | HashMap |
| list | List |
| arraylist | ArrayList |
| collection | Collection |
| iterator | Iterator |
|
类型处理器 |
Java类型 | JDBC类型 |
| BooleanTypeHandler | Boolean,boolean | 任何兼容的布尔值 |
| ByteTypeHandler | Byte,byte | 任何兼容的数字或字节类型 |
| ShortTypeHandler | Short,short | 任何兼容的数字或短整型 |
| IntegerTypeHandler | Integer,int | 任何兼容的数字和整型 |
| LongTypeHandler | Long,long | 任何兼容的数字或长整型 |
| FloatTypeHandler | Float,float | 任何兼容的数字或单精度浮点型 |
| DoubleTypeHandler | Double,double | 任何兼容的数字或双精度浮点型 |
| BigDecimalTypeHandler | BigDecimal | 任何兼容的数字或十进制小数类型 |
| StringTypeHandler | String | CHAR和VARCHAR类型 |
| ClobTypeHandler | String | CLOB和LONGVARCHAR类型 |
| NStringTypeHandler | String | NVARCHAR和NCHAR类型 |
| NClobTypeHandler | String | NCLOB类型 |
| ByteArrayTypeHandler | byte[] | 任何兼容的字节流类型 |
| BlobTypeHandler | byte[] | BLOB和LONGVARBINARY类型 |
| DateTypeHandler | Date(java.util) | TIMESTAMP类型 |
| DateOnlyTypeHandler | Date(java.util) | DATE类型 |
| TimeOnlyTypeHandler | Date(java.util) | TIME类型 |
| SqlTimestampTypeHandler | Timestamp(java.sql) | TIMESTAMP类型 |
| SqlDateTypeHandler | Date(java.sql) | DATE类型 |
| SqlTimeTypeHandler | Time(java.sql) | TIME类型 |
| ObjectTypeHandler | Any | 其他或未指定类型 |
| EnumTypeHandler | Enumeration类型 | VARCHAR-任何兼容的字符串类型,作为代码存储(而不是索引)。 |
public class LimingStringTypeHandler implements TypeHandler {
@Override
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
System.out.println("setParameter - parameter: " + ((String) parameter) + ", jdbcType: " + jdbcType.TYPE_CODE);
ps.setString(i, ((String) parameter));
}
@Override
public Object getResult(ResultSet rs, String columnName) throws SQLException {
System.out.println("getResult - columnName: " + columnName);
return rs.getString(columnName);
}
@Override
public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
System.out.println("getResult - columnIndex: " + columnIndex);
return cs.getString(columnIndex);
}
}
<typeHandlers> <typeHandler javaType="String" jdbcType="VARCHAR" handler="liming.student.manager.type.LimingStringTypeHandler"/> </typeHandlers>
public class LimingObjectFactory extends DefaultObjectFactory {
private static final long serialVersionUID = -399284318168302833L;
@Override
public Object create(Class type) {
return super.create(type);
}
@Override
public Object create(Class type, List<Class> constructorArgTypes, List<Object> constructorArgs) {
System.out.println("create - type: " + type.toString());
return super.create(type, constructorArgTypes, constructorArgs);
}
@Override
public void setProperties(Properties properties) {
System.out.println("setProperties - properties: " + properties.toString() + ", someProperty: " + properties.getProperty("someProperty"));
super.setProperties(properties);
}
}
<objectFactory type="liming.student.manager.configuration.LimingObjectFactory"> <property name="someProperty" value="100"/> </objectFactory>
<mappers> <mapper resource="com/manager/data/maps/UserMapper.xml" /> <mapper resource="com/manager/data/maps/StudentMapper.xml" /> <mapper resource="com/manager/data/maps/ClassMapper.xml" /> </mappers>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有