1. 程式人生 > >mybatis 結合Spring 配置使用

mybatis 結合Spring 配置使用

maven引入mysql,spring,mybatis

package com.test.dao;

import java.util.List;


import com.test.vo.User;

public interface UserNewDao {
	public List<User> Login(User user);

	public User getUsermes(User user);

	public int getUserNum();

	public List<User> getPageUser(long id, int pageSize);

	/*
	 * public void addUser(User user);
	 * 
	 * public void updateUser(User user);
	 * 
	 * public void deleteUser(int UserId);
	 */
}

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
			<scope>runtime</scope>
		</dependency>
<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis-spring.version}</version>
		</dependency>
<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
spring配置web.xml
<!-- 配置Spring的核心監聽器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

建立applicationContext.xml,生成id mybatisuserService物件

beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.test.service" />

	<!-- mybatis配置 -->

	<!-- 配置資料來源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.gjt.mm.mysql.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/usersystem?characterEncoding=utf8&useSSL=true
			</value>
			<!--springmybaitis是我的資料庫 -->
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>wys123</value>
		</property>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>

	<bean id="usernewDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.test.dao.UserNewDao"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>


	<bean id="mybatisuserService" class="com.test.service.MyBatisUserService"></bean>

	<bean id="user" class="com.test.vo.User"></bean>

</beans>

查詢方法:mybatisuserService對應spring中的id

@Resource(name = "mybatisuserService")
	private MyBatisUserService mybatisuserService;
@RequestMapping(value = "user.login", method = RequestMethod.GET)
	public String userLogin(String username,String userpwd, ModelMap modelMap) {
		if (StringUtils.isEmpty(username)) {
			modelMap.addAttribute("usernameerror", "使用者名稱不能為空");
			return "login";
		} else if (StringUtils.isEmpty(userpwd)) {
			modelMap.addAttribute("userpwderror", "密碼不能為空");
			return "login";
		}
		User user=new User();
		user.setUsername(username);
		user.setUserpwd(userpwd);
		user = mybatisuserService.Login(user);
		if (user == null) {
			modelMap.addAttribute("loginerror", "該使用者不存在");
			return "login";
		}
		return "index";
	}
mybatisuserService對應的類
public class MyBatisUserService {
	@Resource(name = "usernewDao")
	private UserNewDao usernewdao;

	/*
	 * public UserNewDao getUsernewdao() { return usernewdao; }
	 * 
	 * public void setUsernewdao(UserNewDao usernewdao) { this.usernewdao =
	 * usernewdao; }
	 */

	public User Login(User user) {
		List<User> userlist = usernewdao.Login(user);
		for (User list : userlist) {
			return list;
		}
		return null;
	}

	public User getUsermes(User user) {
		return usernewdao.getUsermes(user);
	}

	public long getUserNun() {
		return usernewdao.getUserNum();
	}

	public List<User> getPageUser(long id, int pageSize) {
		return usernewdao.getPageUser(id, pageSize);
	}
}
建立mybatis-config.xml
<?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 namespace可以="User“
		<typeAlias alias="User" type="com.test.vo.User" />
	</typeAliases> -->
	
	<mappers>
	      <mapper resource="com/test/dao/UserNewDao.xml" />
	</mappers>
</configuration>

建立Usernew'Dao.xml,namespace對應類名,對應類裡的方法
<mapper namespace="com.test.dao.UserNewDao">
	<select id="Login" parameterType="com.test.vo.User" resultType="com.test.vo.User">
		SELECT * FROM user WHERE username=#{username} AND userpwd=#{userpwd}
	</select>

	<!-- 多對一查詢 -->
	<select id="getUsermes" parameterType="com.test.vo.User"
		resultMap="UserMesPostsMap">
		SELECT user.*,usermes.* FROM user, usermes WHERE
		user.username=#{username} AND
		user.id=usermes.userno
	</select>
	<resultMap type="com.test.vo.User" id="UserMesPostsMap">
		<result property="username" column="username" />
		<result property="id" column="id" />
		<!-- <result property="userpwd" column="userpwd" /> -->
		<association property="usermes" javaType="com.test.vo.Usermes">
			<!-- <collection property="usermes" ofType="com.test.vo.Usermes" > -->
			<result property="userno" column="userno" />
			<result property="username" column="username" />
			<result property="userbalance" column="userbalance" />
			<!-- </collection> -->
		</association>
	</resultMap>

	<select id="getUserNum" resultType="int">
		SELECT count(id) FROM user
	</select>

	<select id="getPageUser" resultMap="UserMesPostsMap">
		SELECT user.*,usermes.* FROM
		user left join usermes on
		usermes.userno=user.id where
		user.id>#{arg0}
		<!-- <if test="arg1 < 0 && arg1==0"> -->
		limit #{arg1}
		<!-- </if> -->
	</select>
	
</mapper> 
建立對應的UserNewDao
package com.test.dao;

import java.util.List;


import com.test.vo.User;

public interface UserNewDao {
	public List<User> Login(User user);

	public User getUsermes(User user);

	public int getUserNum();

	public List<User> getPageUser(long id, int pageSize);

	/*
	 * public void addUser(User user);
	 * 
	 * public void updateUser(User user);
	 * 
	 * public void deleteUser(int UserId);
	 */
}