一.用mybatis的核心配置文件来操作数据库
1.核心配置文件
<?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标签,
mapper.xml也能找到 默认的类别名(可以认为configuration标签中默认有一个typeAliases标签
用来存放系统提供的默认类别名(zijilijie))。
-->
<typeAliases>
<typeAlias alias="User" type="com.java.Bean.User"/>
<typeAlias alias="zidingyi_map" type="java.util.Map"/>
<typeAlias alias="string" type="java.lang.String"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 先定义事务管理器,管理的底层表现为JDBC -->
<transactionManager type="JDBC"/>
<!-- 数据源类型定义为 池类型 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssgl"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<!-- 引入mapper.xml 使在查询的时候,可以对应上相应的接口-->
<mappers>
<mapper resource="com/javaDao/UserMapper.xml"></mapper>
</mappers>
</configuration>
2.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入所需要的约束,用来约定在该xml文件中能够使用的标签,并在写标签的时候有提示功能 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 进行接口方法的映射和实现 -->
<mapper namespace="com.java.Mapper.UserMapper">
<resultMap id="resultMap" type="com.java.Bean.User">
<!-- 这个resultMap仅仅是一个结果集,定义了要返回给user对象的所有属性值,
只有写了sql语句,指明了查询的表名,和所关联的其他表的时候,才能将对应的
字段给对应的属性
-->
<!-- 数据库的字段如果和实体类中属性名称一致时(不区分大小写),那么就可以不定义属性。 -->
<result column="userid" property="userId" javaType="string"></result>
<result column="userpassword" property="userPassword" javaType="string"></result>
<result column="username" property="userName" javaType="string"></result>
<result column="usersex" property="userSex" javaType="string"></result>
<result column="userrole" property="userRole" javaType="string"></result>
<result column="usercollege" property="userCollege" javaType="string"></result>
<result column="usermajor" property="userMajor" javaType="string"></result>
<result column="userclass" property="userClass" javaType="string"></result>
<!-- 站在多方配置一方信息使用association -->
<association property="dormitory" javaType="com.java.Bean.Dormitory">
<result column="dormitoryid" property="dormitoryId"/>
<result column="planpersons" property="planPersons"/>
<result column="realpersons" property="realPersons"/>
<result column="waterprice" property="waterPrice"/>
<result column="powerprice" property="powerPrice"/>
<result column="grade" property="grade"/>
</association>
<!-- 站在一方角度配置多方信息 -->
<collection property="lates" ofType="com.java.Bean.Late">
<result column="lateid" property="lateId"/>
<result column="latetime" property="lateTime"/>
</collection>
</resultMap>
<select id="select_one_user" parameterType="com.java.Bean.User" resultMap="resultMap">
<!-- 在将两个表联系起来以后,如果想访问原来表中的属性,那么直接就可以用表名.属性的方式。
-->
select * from t_user
join t_dormitory
on t_user.dormitoryid=t_dormitory.dormitoryid
join t_late
on t_user.userid=t_late.userid
where t_user.userid=#{userId}
</select>
</mapper>
注意,今天演示了昨天想要演示的内容。如果不把外键删除掉,那么就会出现异常。
并且,即使实体类中的属性没有set,get方法,那么mybatis也能把值传给它。(没用set来fu赋值,而是用反射机制来赋值的)。
二.spring整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>
<!-- 将Mybatis整合到spring中,将Mybatis.xml中的代码,移到spring中 -->
<!-- 配置数据源(dataSource) -->
<bean id = "basicDataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/ssgl?characterEncoding=GBK"/>
<property name="username" value = "root"/>
<property name="password" value = "root"/>
<property name="driverClassName" value = "com.mysql.jdbc.Driver"/>
</bean>
<!-- 配置工厂类 -->
<bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations">
<value>com/javaDao/UserMapper.xml</value>
</property>
<property name="dataSource" ref="basicDataSource"></property>
</bean>
<!-- 进行扫描,将带有 autoWired 的属性自动提供对象 -->
<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value = "com.javaDao"/>
<property name="sqlSessionFactory" ref = "sqlSessionFactory"/>
</bean>
<!-- 服务层 -->
<bean id = "userService" class = "com.java.Service.UserService">
</bean>
</beans>
这里的扫描机制不是太解,不知道要扫哪一个包,导致出错。