总体结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d0b6ceddedc7b526a925ab276fcb10ef.png)
1.bean对象
package com.bjpowernode.pojo;
public class Users {
private Integer userid;
private String username;
private String upass;
@Override
public String toString() {
return "Users{" +
"userid=" + userid +
", username='" + username + '\'' +
", upass='" + upass + '\'' +
'}';
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public Users(Integer userid, String username, String upass) {
this.userid = userid;
this.username = username;
this.upass = upass;
}
public Users() {
}
}
2.jdbc.properties
#jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jz09?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
3.SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
4.applicationContext_mapper.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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="SqlMapConfig.xml"></property>
<property name="typeAliasesPackage" value="com.bjpowernode.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjpowernode.mapper"></property>
</bean>
</beans>
5.applicationContext_service.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">
<import resource="applicationContext_mapper.xml"></import>
<context:component-scan base-package="com.bjpowernode.service.impl"></context:component-scan>
</beans>
6.UserMapper.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.bjpowernode.mapper.UsersMapper">
<insert id="insert" parameterType="Users">
insert into users value (#{userid},#{username},#{upass})
</insert>
</mapper>
7.UserMapper接口
package com.bjpowernode.mapper;
import com.bjpowernode.pojo.Users;
public interface UsersMapper {
int insert(Users users);
}
8.UsersService接口
package com.bjpowernode.service;
import com.bjpowernode.pojo.Users;
public interface UsersService {
int insert(Users users);
}
9.UsersServiceImpl
package com.bjpowernode.service.impl;
import com.bjpowernode.mapper.UsersMapper;
import com.bjpowernode.pojo.Users;
import com.bjpowernode.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
UsersMapper usersMapper;
@Override
public int insert(Users users) {
return usersMapper.insert(users);
}
}