1.创建数据库
create table t_user(
user_id int primary key auto_increment,
user_name varchar(20),
user_age int,
user_address varchar(30)
)
2.导入依赖文件
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- spring_tx -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- MyBatis依赖 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis-spring 整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql数据库驱动 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--druid 阿里的连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.7</version>
</dependency>
3.通过mybatis-generator-core-1.3.6生成文件
- bean类
- 数据访问接口
- SQL映射文件,复制到项目的指定位置
4.创建数据库链接资源文件
mydata.properties
mydriver = com.mysql.jdbc.Driver
myurl = jdbc:mysql://127.0.0.1:3306/test
myusername = root
mypassword = 123456
?5.配置Spring配置文件【aplicationContext.xml】
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置自动扫描包-->
<context:component-scan base-package="com.wangxing.springmybatis"></context:component-scan>
<!--配置加载mydata.properties-->
<context:property-placeholder location="classpath:mydata.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mydrivername}"></property>
<property name="url" value="${myurl}"></property>
<property name="username" value="${myusername}"></property>
<property name="password" value="${mypassword}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:UserBeanMapper.xml"></property>
</bean>
<!--创建数据访问接口对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wangxing.springmybatis.mapper"></property>
</bean>
</beans>
6.Service业务处理层代码
package com.wangxing.springmybatis.service.impl;
import com.wangxing.springmybatis.bean.UserBean;
import com.wangxing.springmybatis.bean.UserBeanExample;
import com.wangxing.springmybatis.mapper.UserBeanMapper;
import com.wangxing.springmybatis.service.UserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserBeanServiceImpl implements UserBeanService {
@Autowired
private UserBeanMapper userBeanMapper;
public void setUserBeanMapper(UserBeanMapper userBeanMapper) {
this.userBeanMapper = userBeanMapper;
}
@Override
public void insertUser(UserBean userBean) {
userBeanMapper.insert(userBean);
}
@Override
public void updateUser(UserBean userBean) {
userBeanMapper.updateByPrimaryKey(userBean);
}
@Override
public void deleteUser(int userid) {
userBeanMapper.deleteByPrimaryKey(userid);
}
@Override
public UserBean selectUserById(int userid) {
return userBeanMapper.selectByPrimaryKey(userid);
}
@Override
public List<UserBean> selectUser() {
return userBeanMapper.selectByExample(null);
}
@Override
public List<UserBean> selectUsernNameAge(UserBean userBean) {
UserBeanExample userBeanExample=new UserBeanExample();
UserBeanExample.Criteria criteria=userBeanExample.createCriteria();
if(userBean.getUserName()!=null && userBean.getUserName().length()>0){
criteria.andUserNameLike("%"+userBean.getUserName()+"%");
}
if(userBean.getUserAge()!=null){
criteria.andUserAgeEqualTo(userBean.getUserAge());
}
List<UserBean> userBeanList=userBeanMapper.selectByExample(userBeanExample);
return userBeanList;
}
}
7.测试控制层代码
import com.wangxing.springmybatis.bean.UserBean;
import com.wangxing.springmybatis.service.UserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
@Controller("userController")
public class UserBeanController {
@Autowired
private UserBeanService userBeanService;
public boolean addUser(UserBean userBean){
boolean flag=false;
try{
userBeanService.insertUser(userBean);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
public boolean updateUser(UserBean userBean){
boolean flag=false;
try{
userBeanService.updateUser(userBean);
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
public UserBean selectUserById(int userid){
UserBean userBean=null;
try{
userBean=userBeanService.selectUserById(userid);
}catch (Exception e){
e.printStackTrace();
}
return userBean;
}
public List<UserBean> selectUser(){
List<UserBean> userBeanList=null;
try{
userBeanList=userBeanService.selectUser();
}catch (Exception e){
e.printStackTrace();
}
return userBeanList;
}
public List<UserBean> selectUserNameAge(UserBean userBean){
List<UserBean> userBeanList=null;
try{
userBeanList=userBeanService.selectUsernNameAge(userBean);
}catch (Exception e){
e.printStackTrace();
}
return userBeanList;
}
public boolean deleteUser(int ids[]){
boolean flag=false;
try{
for(int id:ids){
userBeanService.deleteUser(id);
}
flag=true;
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
无奈源于不够强大
?
|