初学spring,搭建一个工程环境是那么的难
别的不多说
eclipse的安装和meven的安装不做过多解释,百度有很多,
使用eclipse创建一个meven工程
下面就是创建完工程后的目录
选中工程,组合键 alt+enter
1.修改编码 防止引入其他文件出现乱码
2.这个我也不太理解(网上我看很多教程都已经改了)
finish->apply->ok
3.项目 右击->new->source folder 创建src/main/resources 注意不要拼错因为项目默认会从这个文件路径下加载信息
4.pom引入的jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.spring>3.2.9.RELEASE</version.spring>
<version.jackson>2.4.4</version.jackson>
<java.version>1.7</java.version>
</properties>
<dependencys>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency> <!-- 连接池 -->
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency> <!-- for ChainedTransactionManager configuration -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!---->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
</dependencys>
5.在桌面创建一个application.properties文件,这个文件要放到src/main/resources路径下,这个文件不用任何关联,spring会自动扫描到.properties文件,
这里主要是对数据库的一些链接操作
6.在app.class中写spring的启动 注意:注解不要写错
7.创建DataSourecConfig.Class类 这个类主要是数据库的连接
8.在src/main/java创建一个包 com.love.mapper 主要是放置接口
9.在src/main/resource创建一个文件夹起名为mybatis 主要放置数据库的xml文件
10.在DataSourecConfig中编辑
@Configuration
@MapperScan(basePackages =DateSourceConfig.PACKAGE, sqlSessionFactoryRef = "rdsSqlSessionFactory")
public class DateSourceConfig {
static final String PACKAGE = "com.love.mapper";//接口的包地址
@Value("${my_mysql_url}")//从application.properties取到的值
private String dbUrl;
@Value("${my_mysql_username}")//从application.properties取到的值
private String dbUser;
@Value("${my_mysql_password}")//从application.properties取到的值
private String dbPassword;
@Bean(name = "rdsDataSource")
@Primary
public DataSource rdsDataSource() {
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
System.out.println("dbUrl "+dbUrl);
return dataSource;
}
@Bean(name = "rdsTransactionManager")
@Primary
public DataSourceTransactionManager rdsTransactionManager() {
return new DataSourceTransactionManager(rdsDataSource());
}
@Bean(name = "rdsSqlSessionFactory")
@Primary
public SqlSessionFactory rdsSqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(rdsDataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return sessionFactory.getObject();
}
}
11.在src/main/java的com/love.you包中创建init.class类这个主要是监听spring是否run成功,充公后可以进行一些初始化操作
@Component
public class init implements ApplicationListener<ContextRefreshedEvent>{
public void onApplicationEvent(ContextRefreshedEvent arg0) {
final ApplicationContext app = arg0.getApplicationContext();
System.out.println("在此初始化某些数据***");
}
}
好了 初期的搭建就完成了 下面然我们来测试一下
创建好如下的包
首先在mysql创建一个user表,int类型的id作为主键自增长 varchar类型的name,int类型的age
在mapper包中创建user.class
public interface userMapper {
public void addUserInfo(@Param("name")String name,@Param("age")int age);
}
在Model包中创建UserModel.class
public class UserModel {
public String name;
public int age;
public UserModel(){
super();
}
public UserModel(String name,int age){
this.name = name;
this.age = age;
}
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;
}
}
在services包中创建UserService.class类 为mapper的实现类
@Service
public class UserService {
@Autowired
private userMapper userMapper;
public void addUserInfo(String name,int age){
userMapper.addUserInfo(name, age);
}
}
在controller中创建UserController.class 实现一个GRT方法
@Controller
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/adduser",method = RequestMethod.GET)
@ResponseBody
public void adduser(){
userService.addUserInfo("CZH", 18);
}
}
在src/main/resources中的mybatis创建一个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.love.mapper.userMapper"> //注意namespace要写对
<resultMap type="com.love.model.UserModel" id="userMap">//注意type要写对,区分大小写
<result property="name" column="name" />
<result property="age" column="age" />
</resultMap>
<insert id="addUserInfo" parameterType="com.love.model.UserModel">//id是mapper的方法名
insert into demosql (name,age)values(#{name},#{age})
</insert>
</mapper>
最后重要的一点就是在app.class中的@SpringBootApplication(scanBasePackages={"com.love"})
这是扫描所有的前缀为com.love的包
好了运行工程把 右击项目run as ->meven build...
运行成功后在浏览器中输入地址http://localhost:8070/api/adduser
大功告成 看我的数据库信息 哈哈 真不容易啊
注册web页面
在src/main/resources创建文件夹templates,在这个文件夹中创建HTML页面
右击templates文件夹-->new-->other-->web-->HTML
在controller包中创建controllerconfig类,继承WebMvcConfigurerAdapter
@Configuration
public class ControllerConfig extends WebMvcConfigurerAdapter {
//注册web页面
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//hello为HTML页面的名字
registry.addViewController("hello").setViewName("hello");
}
}
打开浏览器 http://localhost:8070/hello
如果有什么疑问请留言........ 有错误的地方希望能够指出来 我在加以改正
附加:链接: http://pan.baidu.com/s/1i4Vh3V3 密码: e4g7
链接: http://pan.baidu.com/s/1eRVnDvG 密码: yadf