mybatis

 

POM

 

      
      <!-- Mybatis -->
        <mybatis.version>3.2.3</mybatis.version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
   <mysql.version>5.1.10</mysql.version>

<!-- spring-tx和spring-jdbc为tk mybatis用 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.3.9</version> </dependency> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency>

 

 

 

spring配置


<!-- 数据源配置 -->
<!-- bcc数据库 -->
<bean id="bccDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url"
value="${bcc.jdbc.url}?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull" />
<property name="username" value="${bcc.jdbc.username}" />
<property name="password" value="${bcc.jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxWait" value="30" />
<property name="minIdle" value="1" />
<property name="poolPreparedStatements" value="true" />
<property name="validationQuery" value="select now()" />
</bean>


<bean id="bcc_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="bccDataSource" />
</bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.breath.dao.bcc" />
<property name="sqlSessionFactoryBeanName" value="bcc_sqlSessionFactory" />
</bean>


<bean id="bcc_transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="bccDataSource" />
</bean>
<!-- bcc数据库END -->



<!-- 公共部分 -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isea533.mybatis.mapper" />
</bean>

<!-- 配置数据源END -->

 

 

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/thirdserver
jdbc.username=root
jdbc.password=root123

 

mapper:

public interface UserMapper extends Mapper<User> {

@Select("SELECT * FROM user WHERE username = #{username}")
User getUser(@Param(value = "username") String name);

@Insert("INSERT INTO user(username,password,type,nickName) VALUES(#{name}," 
+ "#{password},#{type},#{nickName})") 
public void addNewUser(User user); 

 @SelectProvider(type = Provider.class, method = "selectAll")
  List<User> selectMembers();

class Provider {

public String selectAll() {
StringBuffer sql = new StringBuffer();
sql.append("SELECT * from");
sql.append(" tb_user");
return sql.toString();
}

}

 

}
 

dao

package com.yibai.spring.bean;

import java.io.Serializable;


public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6537166060616535900L;

	private String name;
	private String password;
	private String type;
	private String nickName;

	public String getName() {
		return name;
	}

	public void setUsername(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
}

  

service

package com.yibai.spring.service;

import java.util.Map;

import com.yibai.spring.bean.User;
import com.yibai.spring.common.EmResult;
import com.yibai.spring.mapper.UserMapper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;




@Service
public class UserService {
	private static final Logger logger=LoggerFactory.getLogger(UserService.class);
	
	 @Autowired 
	 private UserMapper userMapper;
	public EmResult register(Map<String, String> userMap){
		String mapJson=JSON.toJSONString(userMap);
		logger.info("mapJson="+mapJson);
		User user=new User();
		user=JSON.parseObject(mapJson, User.class);
		logger.info("json对象转换成功,NickName="+user.getNickName());		
		
		try{
		logger.info("开始查询数据库操作");
		
		User search=userMapper.getUser(user.getName());
		if(search!=null&&search.getName().equals(user.getName())){
			return new EmResult("00000","操作成功",user.getName()+"用户已经存在");
		}
		userMapper.addNewUser(user);
		}catch(Exception e){
			e.printStackTrace();
			return new EmResult("00000","操作失败");
		}finally{
			
		}
		
		return new EmResult("00000","操作成功");
	}
	
	public EmResult queryUser(String username){
		
		try{
		logger.info("开始查询数据库操作");
		User user=userMapper.getUser("mayun");
		if(user!=null){
			return new EmResult("00000","查询成功",user);
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			
		}
		return new EmResult("00000","查询成功","未查询到该用户");
		
	}
}

  

 

posted @ 2018-06-18 22:27  M兔  阅读(261)  评论(0编辑  收藏  举报