码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis

时间:2018-06-18 22:43:29      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:imp   pool   entity   pre   frame   XA   time   SQ   iba   

 

POM

 

<!-- mybatis版本号 -->
<mybatis.version>3.2.1</mybatis.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>
		</dependency>

		<!-- mybatis/spring包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.0</version>
		</dependency>

		<!-- mysql驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.29</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- mybatis的通用mapper的通用Example使用 -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>3.2.0</version>
		</dependency>
		<!-- tk.mybatis依赖项:项目依赖于JPA的注解,需要添加Maven依赖: -->
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>

  

 

spring配置

<!-- 配置数据源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${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="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yibai.spring.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
    
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yibai.spring.*"/>
    </bean>
    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

 

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

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","查询成功","未查询到该用户");
		
	}
}

  

 

mybatis

标签:imp   pool   entity   pre   frame   XA   time   SQ   iba   

原文地址:https://www.cnblogs.com/peak911/p/9196556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!