创建数据库mybatis并创建表customer
创建maven Web工程spring05并导入依赖
<!--springwebmvc可以导入spring全部核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
在resources下创建database.properties添加数据库连接池配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=x5
jdbc.minIdle=45
jdbc.maxIdle=50
jdbc.initialSize=5
jdbc.maxActive=100
jdbc.maxWait=100
jdbc.removeAbandonedTimeout=180
jdbc.removeAbandoned=true
在resources下创建spring-mybatis.xml 添加数据库连接池并配置spring扫描包、配置sqlsession工厂和mapper动态代理
<?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.lijunbo.*"/>
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" scope="singleton">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
<property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
<property name="typeAliasesPackage" value="com.lijunbo.pojo" ></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--扫描映射文件(mapper动态代理)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lijunbo.dao"/>
</bean>
</beans>
在resources下创建mapper文件夹并在mapper下创建customerMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lijunbo.dao.CustomerDao">
<select id="query" resultType="com.lijunbo.pojo.Customer">
select * from customer
</select>
<insert id="add">
insert into customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
</insert>
</mapper>
创建Customer实体类添加get/set方法
package com.lijunbo.pojo;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", username='" + username + '\'' +
", jobs='" + jobs + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
创建dao层
package com.lijunbo.dao;
import com.lijunbo.pojo.Customer;
import java.util.List;
public interface CustomerDao {
public List<Customer> query();
public void add(Customer customer);
}
创建service层
package com.lijunbo.service;
import com.lijunbo.pojo.Customer;
import java.util.List;
public interface CustomerService {
public List<Customer> query();
public void add(Customer customer);
}
创建serviceimpl
package com.lijunbo.service.impl;
import com.lijunbo.dao.CustomerDao;
import com.lijunbo.pojo.Customer;
import com.lijunbo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDao dao ;
public List<Customer> query() {
List<Customer> list = dao.query();
return list;
}
public void add(Customer customer) {
dao.add(customer);
}
}
创建controller层
package com.lijunbo.controller;
import com.lijunbo.pojo.Customer;
import com.lijunbo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
@Controller
public class CustomerController {
@Autowired
CustomerService service;
public String query(){
List<Customer> list = service.query();
System.out.println(list);
return null;
}
public String add(Customer customer){
service.add(customer);
return null;
}
}
创建测试类测试
package com.lijunbo;
import com.lijunbo.controller.CustomerController;
import com.lijunbo.pojo.Customer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
CustomerController controller = (CustomerController) context.getBean("customerController");
Customer customer = new Customer();
customer.setUsername("李俊博");
customer.setPhone("111111");
customer.setJobs("java");
controller.add(customer);
controller.query();
}
}
测试成功