mybatis 结合Spring 配置使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shengsheng1_1/article/details/79699285

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);
	 */
}




阅读更多

没有更多推荐了,返回首页