-
首先是安装软件
mysql安装
navicat for mysql 11版本 (需要联系我,百度云文件满了,无法分享)
-
工程搭建
具体基础搭建先参考ssm框架入门
-
1.增加导包(图一)
图一
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!--jdbc连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--Gson-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
注意:因为我数据库是安装8.0以上,所以为了匹配,mysql-connector-java也使用8.0以上,使用低版本有可能连接不上数据库
-
2.建立数据库
使用navicat 建立localhost ,创建test数据库,建立User表(图二,图三,图四,图五,图六,图七,图八)
图二
图三
图四
图五
图六 -
3.在resources 中建立jdbc.properties文件,并编写代码
图七g
图八
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=数据库密码
#最大连接数
maxPoolSize=20
#最小连接数
minPoolSize=10
#连接超时时间
checkoutTimeout=60000
#失败重连次数
acquireRetryAttempts=3
注意:数据库密码是安装时输入的密码,需要修改,jdbcd url3306/后面是连接的数据库名称
Tip:mysql-connector-java 版本8.0以上是com.mysql.cj.jdbc.Driver,以下是com.mysql.jdbc.Driver
-
4.在resources 中建立mybatis-config.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>
<!-- 使⽤jdbc的getGeneratedKeys获取数据库⾃增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使⽤列别名替换列名 默认:true -->
<setting name="useColumnLabel" value="true"/>
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
-
5.在resources中建立文件夹 mapper,并在mapper中创建UserDao.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.sm.dao.UserDao">
<select id="getAllUsers" resultType="UserModel">
select * from user
</select>
<select id="getUser" resultType="UserModel">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="UserModel">
<!--ignore忽略自动增长的主键id-->
insert ignore into user (name, age) values (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="UserModel">
update user set name=#{name} where id=#{id}
</update>
<delete id="deleteUser" parameterType="UserModel">
delete from user where id=#{id}
</delete>
</mapper>
注意:namespace必须与自己的包名一致(图十一)
-
6.applicationContext.xml中添加代码
图十一
<!--配合MyBatis-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置连接池属性-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="${maxPoolSize}"/>
<property name="minPoolSize" value="${minPoolSize}"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="${checkoutTimeout}"/>
<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置 MyBaties 全局配置⽂件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描 model 包 使⽤别名 -->
<property name="typeAliasesPackage" value="com.sm.model"/>
<!-- 扫描 sql 配置⽂件:mapper 需要的 xml ⽂件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置扫描 dao 接⼝包, 动态实现 dao 接⼝, 注⼊到 Spring 容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注⼊ sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描 dao 接⼝包 -->
<property name="basePackage" value="com.sm.dao"/>
</bean>
<!--spring-service-->
<context:component-scan base-package="com.sm.service"/>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注⼊数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
</bean>
-
7.在model下建立UserModel.java文件(图十二)
图十二
public class UserModel {
private int id;
private String name;
private int age;
public UserModel() {
}
public UserModel(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
-
8.在文件夹dao下建立UserDao.java文件,并编写代码(图十三)
图十三
List<UserModel> getAllUsers();
UserModel getUser(String id);
boolean addUser(UserModel userModel);
boolean updateUser(UserModel userModel);
boolean deleteUser(UserModel userModel);
-
9.在文件夹service下建立UserService.java文件并编写代码(图十四)
图十四
@Service
@Transactional
public class UserService {
@Autowired
private UserDao userDao;
public List<UserModel> getAllUsers() {
return userDao.getAllUsers();
}
public UserModel getUser(String id) {
return userDao.getUser(id);
}
public boolean addUser(UserModel userModel) {
return userDao.addUser(userModel);
}
public boolean updateUser(UserModel userModel) {
return userDao.updateUser(userModel);
}
public boolean deleteUser(UserModel userModel) {
return userDao.deleteUser(userModel);
}
}
Tip: 如果引入的UserDao报错,可以不用管,强迫症 (图十五)
图十五
-
10.在文件夹controller下建立UserController.java文件并编写代码(图十六)
图十六
@RestController
@RequestMapping("/user")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "getAllUsers", method = RequestMethod.GET)
public List<UserModel> getAllUsers() {
return userService.getAllUsers();
}
@RequestMapping(value = "getUser", method = RequestMethod.GET)
public UserModel getUser(String id) {
return userService.getUser(id);
}
@RequestMapping(value = "addUser", method = RequestMethod.GET)
public String addUser(UserModel userModel) {
boolean flag = userService.addUser(userModel);
if (flag) {
return "数据录入成功";
} else {
return "数据录入失败";
}
}
}
最后启动tomcat,输入http://localhost:8080/user/getAllUsers
成功啦!!!!!
网友评论