1. 程式人生 > >如何通過Spring和MyBatis的整合實現資料庫表中資訊查詢?

如何通過Spring和MyBatis的整合實現資料庫表中資訊查詢?


UserMapper.java

package cn.kgc.dao;

import java.util.List;

import cn.kgc.pojo.User;

public interface UserMapper {
   
	 //根據條件(使用者名稱稱、角色ID)查詢使用者列表
	public List<User> getUserList(User user);
}

UserMapperImpl.java

package cn.kgc.dao;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.kgc.pojo.User;

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{


	@Override
	public List<User> getUserList(User user) {
		                  // 面向介面程式設計思想:UserMapper介面的全類名+介面中的方法名(同select中的id)
		return this.getSqlSession().selectList("cn.kgc.dao.UserMapper.getUserList", user);

	}

}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<!-- namespace表示xml全類名  -->
<mapper namespace="cn.kgc.dao.UserMapper">
     
       <!-- 根據條件(使用者名稱稱、角色ID)查詢使用者列表
      -->
      
      <select id="getUserList"  parameterType="User" resultMap="userlist">
          select u.*,r.roleName 
          from smbms_user u,smbms_role r
          where u.userName like CONCAT('%',#{userName},'%')
          and u.userRole=#{userRole}
          and u.userRole=r.id
          
      </select>
      
       <resultMap type="User" id="userlist">
      	 <result property="id" column="id"/>
      	 <result property="userCode" column="userCode"/>
      	 <result property="userName" column="userName"/>
      	 <result property="phone" column="phone"/>
      	 <result property="birthday" column="birthday"/>
      	 <result property="gender" column="gender"/>
      	 <result property="userRole" column="userRole"/>
      	 <result property="userRoleName" column="roleName"/>      	 
      </resultMap>
      
</mapper>

UserTest.java

package cn.kgc.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.User;

public class UserTest {

	@Test
	public void test() {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
			//介面的引用
		UserMapper userMapper=(UserMapper)ctx.getBean("userMapper");
		List<User>userList=new ArrayList<User>();
		User u=new User();
		u.setUserName("趙");
		u.setUserRole(3);
			//呼叫介面中的方法
		userList=userMapper.getUserList(u);
		System.out.println(userList);
		for(User uu : userList){
			System.out.println(uu.getUserCode()+"------"+uu.getUserName()+"----"+uu.getUserRole()+"----"+uu.getUserRoleName()+"-----"+uu.getAddress());
		}
	}

}

MyBatisUtil.java 無用

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
     <!-- 配置資料來源 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
          <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms"></property>
          <property name="username" value="root"></property>
          <property name="password" value="root"></property>
     </bean>
   
   
   <!--  配置SqlSessionFactoryBean      為我們mybatis提供的一個整合外掛包-->
       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 引用資料來源元件 -->
            <property name="dataSource" ref="dataSource"></property>
            
            <!--引用MyBATIS配置檔案中的資訊   -->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            
            
            <!--配置sql對映檔案資訊  -->
            <property name="mapperLocations">
            		<list>
            			<value>classpath:cn/kgc/dao/UserMapper.xml</value>
            		</list>
            </property>
       </bean>
       
      
      <!-- 配置DAO -->
      <bean id="userMapper" class="cn.kgc.dao.UserMapperImpl">
           <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
      </bean>
        
</beans>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>

   <typeAliases>
   		<package name="cn.kgc.pojo"/>
   </typeAliases>
   
 
</configuration>