标签: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&characterEncoding=UTF-8&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","查询成功","未查询到该用户"); } }
标签:imp pool entity pre frame XA time SQ iba
原文地址:https://www.cnblogs.com/peak911/p/9196556.html