好多天没有发博客了,下面就直接说下我正在学习的mybatis。
下面就说下mybatis在spring中最简单的配置:
<!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 基本属性 url、user、password --> <property name="url" value="${dataSource.url}" /> <property name="username" value="${dataSource.username}" /> <property name="password" value="${dataSource.password}" /> <property name="driverClassName" value="${dataSource.driverClassName}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/spring/mapping/*.xml" /><!-- 实体映射文件 --> <property name="configLocation" value="/WEB-INF/configuration.xml" /><!-- 配置别名 --> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.spring.dao" /><!-- 扫描dao层文件 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
对于这些配置,学过hibernate的一看就能明白了,基本就是配置数据源,以及实体映射,和事务的一些配置。
下面是实体的内容:
public class User{ public int id; public String name; public String password; public Role role; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } }
public class Role{ private int roleid; private String rolename; public int getRoleid() { return roleid; } public void setRoleid(int roleid) { this.roleid = roleid; } public String getRolename() { return rolename; } public void setRolename(String rolename) { this.rolename = rolename; } }
user.xml
<?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.spring.dao.LoginDao"> <resultMap id="BaseResultMap" type="User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <association property="role" javaType="Role" > <id column="roleid" property="roleid" jdbcType="INTEGER" /> <result column="rolename" property="rolename" jdbcType="VARCHAR" /> </association> </resultMap> <select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String"> select * from user as u,role as r where name = #{name} and u.roleid=r.roleid and u.roleid=1 </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="User" type="com.spring.entity.User" /> <typeAlias alias="Role" type="com.spring.entity.Role" /> </typeAliases> </configuration>
基本的配置就这些,有不明白的直接问,我会为尽力为大家解答,细节问题我没有细说。
时间: 2024-10-12 07:50:58