使用eclipse来完成Springboot整合SSM框架
Springboot整合SSM框架文件版
从官网下载工程压缩文件并导入eclipse中
从https://start.spring.io/此网址下直接下载工程的压缩文件
在src/main/resources目录下创建mybatis-config.xml
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>
<!-- 获取主键自增的值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 开启驼峰命名的转换 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
在src/main/java下创建com.sikiedu.ssm.entity
User.java
package com.sikiedu.ssm.entity;
public class User {
private Integer id;
private String username;
private String password;
/*get和set方法*/
}
在src/main/java下创建com.sikiedu.ssm.dao
UserDao.java
package com.sikiedu.ssm.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.sikiedu.ssm.entity.User;
public interface UserDao {
//增
int addUser1(String username,String password);
int addUser2(User user);
//删
int deleteUserById2(@Param("Lain")int xxx);//起别名
int deleteUserById1(int id);
//改
int updateUser(User user);
//查
List<User> searchAllUsers();
User searchUserById(int id);
}
在src/main/resources目录下创建mapper文件夹
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.sikiedu.ssm.dao.UserDao">
<!-- 返回类型是com.sikiedu.ssm.entity.User -->
<select id="searchAllUsers" resultType="com.sikiedu.ssm.entity.User">
select * from user
</select>
<select id="searchUserById" resultType="com.sikiedu.ssm.entity.User">
select * from user where id = #{id}
</select>
<!-- useGeneratedKeys如果执行成功则返回主键的值 -->
<insert id="addUser1" useGeneratedKeys="true">
insert into user(username,password) values(#{username},#{password})
</insert>
<!-- parameterType入参类型 keyProperty必须包含在参数里-->
<insert id="addUser2" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.sikiedu.ssm.entity.User">
insert into user values(#{id},#{username},#{password})
</insert>
<update id="updateUser" parameterType="com.sikiedu.ssm.entity.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUserById1">
delete from user where id=#{id}
</delete>
<delete id="deleteUserById2">
delete from user where id=#{Lain}
</delete>
</mapper>
在src/main/java下创建com.sikiedu.ssm.config
SSMConfiguration.java
package com.sikiedu.ssm.config;
import java.beans.PropertyVetoException;
import java.io.IOException;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
//mapper的扫描路径
@MapperScan("com.sikiedu.ssm.dao")
public class SSMConfiguration {
@Autowired
private DataSource dataSource;
//dataSource
@Bean(name="dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//driver
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
//url
dataSource.setJdbcUrl("jdbc:mysql://::1:3306/ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true");
//username
dataSource.setUser("login");
//password
dataSource.setPassword("123");
//在关闭连接后不会自动的commit
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
//sessionFactory
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//加载主配置文件mybatis-config.xml
bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
//Mapper扫描路径
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = "classpath*:/mapper/**.xml";
bean.setMapperLocations(resolver.getResources(packageSearchPath));
//配置实体的包
bean.setTypeAliasesPackage("com.sikiedu.ssm.entity");
//dataSource
bean.setDataSource(dataSource);
return bean;
}
}
在src/main/java下创建com.sikiedu.ssm.controller
TestController.java
package com.sikiedu.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sikiedu.ssm.dao.UserDao;
import com.sikiedu.ssm.entity.User;
@Controller
public class TestController {
@Autowired
private UserDao userDao;
@RequestMapping("/test")
public String test() {
// List<User> searchAllUsers = userDao.searchAllUsers();
//
// for (User user : searchAllUsers) {
// System.out.println(user.getId()+"|"+user.getUsername()+'|'+user.getPassword());
//
// }
//
// User user = userDao.searchUserById(2);
// System.out.println(user.getId()+"|"+user.getUsername()+'|'+user.getPassword());
// User user = new User();
// user.setId(5);
// user.setUsername("tianjinfeng");
// user.setPassword("789");
// userDao.addUser2(user);
// userDao.addUser1("jifeng", "852");
// User user = userDao.searchUserById(5);
// user.setUsername("siki");
// user.setPassword("555");
// userDao.updateUser(user);
// userDao.deleteUserById1(5);
userDao.deleteUserById2(2);
return "hello";
}
}
Springboot整合SSM框架注解版
使用官网来创建Maven工程
在src/main/resources下的application.properties配置
spring.datasource.url=jdbc:mysql://::1:3306/ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true
spring.datasource.username=login
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
在src/main/java下创建com.sikiedu.springbootssm.entity
User.java
package com.sikiedu.ssm.entity;
public class User {
private Integer id;
private String username;
private String password;
/*get和set方法*/
}
在src/main/java下创建com.sikiedu.springbootssm.dao
UserMapper.java
package com.sikiedu.springbootssm.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.sikiedu.springbootssm.entity.User;
import com.sikiedu.springbootssm.provider.UserProvider;
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
@Select("select * from user where id = #{id}")
User findUserById(int id);
// @Insert("insert into user(username,password) values(#{username},#{password})")
// void save(User user);
@InsertProvider(type=UserProvider.class,method="saveUser")
void save(User user);
@DeleteProvider(type=UserProvider.class,method="deleteUser")
void deleteUser(User user) throws Exception;
@Update("update user set username=#{username},password=#{password} where id=#{id}")
void modifyUser(User userInSQL);
}
在src/main/java下创建com.sikiedu.springbootssm.provider
UserProvider.java
package com.sikiedu.springbootssm.provider;
import com.sikiedu.springbootssm.entity.User;
public class UserProvider {
public String saveUser(User user) {
if(user.getId()==null) {
return "insert into user(username,password) values(#{username},#{password})";
}else {
return "insert into user values(#{id},#{username},#{password})";
}
}
public String deleteUser(User user) {
if(user.getId() != null) {
return "delete from user where id=#{id}";
}else if(user.getUsername() != null){
return "delete from user where username=#{username}";
}else if(user.getPassword() != null) {
return "delete from user where password=#{password}";
}else {
throw new RuntimeException("请输入用户名或者密码或者ID");
}
}
}
在src/main/java下新建com.sikiedu.springbootssm.controller
TestController.java
package com.sikiedu.springbootssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sikiedu.springbootssm.dao.UserMapper;
import com.sikiedu.springbootssm.entity.User;
@Controller
public class TestController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/findAll")
public String findAll() {
List<User> userList = userMapper.findAll();
for (User user : userList) {
System.out.println(user.getId()+"|"+user.getUsername()+"|"+user.getPassword());
}
return "xx";
}
//查
@RequestMapping("/findUserById")
public String findUserById(int id) {
User user = userMapper.findUserById(id);
System.out.println(user.getId()+"|"+user.getUsername()+"|"+user.getPassword());
return "xx";
}
//增
@RequestMapping("/addUser")
public String addUser(User user) {
userMapper.save(user);
return "xx";
}
//删
@RequestMapping("/deleteUser")
public String deleteUser(User user) {
try {
userMapper.deleteUser(user);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return "XX";
}
//改
@RequestMapping("/modifyUser")
public String modifyUser(User user) {
User userInSQL = userMapper.findUserById(user.getId());
userInSQL.setUsername(user.getUsername());
userInSQL.setPassword(user.getPassword());
userMapper.modifyUser(userInSQL);
return "xx";
}
}